Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayerStatus for local file not ready

I'm having some trouble with playback of a local .wav file. My AVPlayerStatus is printing 0, which i'm assuming means it's not ready to play. I can't seem to figure it out but having said that, this is my first time trying to use anything to do with AVPlayer so it's probably something simple. I'd appreciate any help. Here's the code:

i should add that my sounds are not playing!

-(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");
}
like image 243
garethdn Avatar asked May 16 '12 11:05

garethdn


1 Answers

Here's some important gotchas Apple is pointing out when using AVPlayItem -initWithURL: method:

Special Considerations

This method immediately returns the item, but with the status AVPlayerItemStatusUnknown.

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

If the URL contains no valid data or otherwise can't be used by the player item, the status later changes to AVPlayerItemStatusFailed.

What I imagine is happening is, because you are sending a -play message not long after you create the AVPlayerItem object, its status is still AVPlayerItemStatusUnknown (same goes for the AVPlayer object) since it didn't receive enough time to load the resource and change status to AVPlayerItemStatusReadyToPlay.

My sugestions:

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

OR, even simpler,

  • Load an AVAsset or an AVURLAsset object from the NSURL and use the AVPlayerItem –initWithAsset: method, which should return a ready to play object, and then you call call -play immediately after, if you so please.
like image 107
MiguelB Avatar answered Sep 22 '22 23:09

MiguelB