iphone – 用于本地文件的AVPlayerStatus尚未就绪

我在播放本地.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");
}
最佳答案
以下是Apple在使用AVPlayItem -initWithURL:方法时指出的一些重要问题:

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.

我想象的是,因为你在创建AVPlayerItem对象后不久发送了一个-play消息,它的状态仍然是AVPlayerItemStatusUnknown(同样适用于AVPlayer对象),因为它没有足够的时间来加载资源和将状态更改为AVPlayerItemStatusReadyToPlay.

我的sugestions:

>检查AVPlayerItem / AVPlayer状态更改和稍后调用播放;

或者,甚至更简单,

>从NSURL加载AVAssetAVURLAsset对象并使用AVPlayerItem -initWithAsset:方法,该方法应返回准备播放对象,然后如果您愿意,请立即调用call -play.

转载注明原文:iphone – 用于本地文件的AVPlayerStatus尚未就绪 - 代码日志