我在播放本地 .wav 文件时遇到了一些问题。我AVPlayerStatus
正在打印0
,我假设这意味着它还没有准备好播放。我似乎无法弄清楚,但话虽如此,这是我第一次尝试使用与 AVPlayer 相关的任何东西,所以它可能很简单。我会很感激任何帮助。这是代码:
我应该补充一点,我的声音没有播放!
-(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");
}