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");
}
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:
-play
at a later stage;OR, even simpler,
–initWithAsset:
method, which should return a ready to play object, and then you call call -play
immediately after, if you so please.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With