AVPlayerStatus for local file not ready

I am having trouble playing a local WAV file. My AVPlayerStatus prints 0 , which I assume is not ready for playback. I cannot understand this, but having said that, I am trying for the first time to use something that is related to AVPlayer, so maybe something simple. I would appreciate any help. Here is the code:

I must add that my sounds do not play!

 -(void) playWordSound:(UILabel *)label { NSString *path; switch (label.tag) { case 1: path = [[NSBundle mainBundle] pathForResource:@"humpty" ofType:@"wav"]; break; case 2: path = [[NSBundle mainBundle] pathForResource:@"dumpty" ofType:@"wav"]; break; case 3: path = [[NSBundle mainBundle] pathForResource:@"saty" ofType:@"wav"]; break; case 4: path = [[NSBundle mainBundle] pathForResource:@"on 2" ofType:@"wav"]; break; case 5: path = [[NSBundle mainBundle] pathForResource:@"a 3" ofType:@"wav"]; break; case 6: path = [[NSBundle mainBundle] pathForResource:@"wall" ofType:@"wav"]; break; } NSURL *url = [NSURL fileURLWithPath:path]; AVQueuePlayer *player; static const NSString *ItemStatusContext; AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; [playerItem addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext]; player = [AVPlayer playerWithPlayerItem:playerItem]; NSLog(@"%d",player.status); [player play]; } - (void)playerItemDidReachEnd:(NSNotification *)notification { NSLog(@"Sound finished"); } 
+6
source share
2 answers

Here are some important errors Apple points out when using AVPlayItem - the initWithURL: method:

Special considerations

This method returns an element immediately, but with the status AVPlayerItemStatusUnknown.

If the URL contains valid data that can be used by a player item, the status later changes to AVPlayerItemStatusReadyToPlay.

If the URL does not contain reliable data or the player cannot be used in any other way, the status will later be changed to AVPlayerItemStatusFailed.

I think this happens because you send the -play message shortly after creating the AVPlayerItem object, its status is still AVPlayerItemStatusUnknown (the same applies to the AVPlayer object), since it did not get enough time to load the resource and change the status to AVPlayerItemStatusReadyToPlay .

My suggestions:

  • Or check the status change of AVPlayerItem / AVPlayer and call -play at a later stage;

OR, even easier

  • Download the AVAsset or AVURLAsset of the object from NSURL and use the AVPlayerItem –initWithAsset: method, which should return the ready-to-play object, and then you call -play right after if you want to.
+11
source

0 equivalent to AVPlayerStatusUnknown , I assume that this is because the player is not trying to load the URL until you really call play .

From the documentation :

AVPlayerStatusUnknown

Indicates that the status of the player is still unknown, since it did not try to load new media resources for playback.

+1
source

Source: https://habr.com/ru/post/915902/


All Articles