avplayeritem总是处于未知状态

时间:2015-03-18 16:54:07

标签: ios bundle avplayer avasset avplayeritem

我刚发现一个非常奇怪的问题,

从本地存储加载mp3文件时

avplayeritem始终处于未知状态,如果文件放在文件夹中,则无法播放。

方式1:

//AVAsset *asset = [[AVURLAsset alloc] initWithURL:_URL options:nil];
//AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:_URL];
//line 1
while (playerItem.status!=AVPlayerItemStatusReadyToPlay
       && playerItem.status!=AVPlayerItemStatusFailed) {
    NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status);
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} 
//line 2

NSLog(@"avplayer status: %d item: %@", playerItem.status, _URL);
NSArray *metadata = [playerItem.asset commonMetadata];

方式2:

AVAsset *asset = [[AVURLAsset alloc] initWithURL:_URL options:nil];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
/*
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:_URL];
//line 1
while (playerItem.status!=AVPlayerItemStatusReadyToPlay
       && playerItem.status!=AVPlayerItemStatusFailed) {
    NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status);
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} 
//line 2
 */
NSLog(@"avplayer status: %d item: %@", playerItem.status, _URL);
NSArray *metadata = [playerItem.asset commonMetadata];

输出:

2015-03-19 00:42:49.243 goodnightfm[6737:353911] avplayer: <AVPlayerItem: 0x7fdc3bbd5c00, asset = <AVURLAsset: 0x7fdc3bbd8a50, URL = file:///Users/galenzhao/Library/Developer/CoreSimulator/Devices/123A77A7-DC61-4795-8D9A-E71002E261DA/data/Containers/Data/Application/4CCEED79-1D5A-4D00-BC5E-FC52BD5393F9/Documents/CMStorage/70efdf2ec9b086079795c442636b55fb>> status: 0

但是如果mp3文件在应用程序包中,那么way2代码也可以运行,即使状态仍然是未知的,

2015-03-19 00:45:35.977 goodnightfm[6783:355678] avplayer status: 0 item: file:///Users/galenzhao/Library/Developer/CoreSimulator/Devices/123A77A7-DC61-4795-8D9A-E71002E261DA/data/Containers/Bundle/Application/F2446C29-49F3-4B92-A7B0-7EFCC1A19274/goodnightfm.app/demo5.mp3

我确定app bundle&amp; document中的文件是同一个文件,它可以在任何其他软件中播放,

通过使用way2,捆绑文件和文档文件之间唯一不同的是,

如果mp3文件放在bundle中,这个函数可以返回元数据

NSArray *metadata = [playerItem.asset commonMetadata];

但使用文档网址

返回nil

1 个答案:

答案 0 :(得分:2)

首先,你需要在玩家项目中添加一个观察者来适当地检查它的状态:

[playerItem addObserver:self 
             forKeyPath:@"status"
                options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                context:nil];

或者将观察者添加到AVPlayer对象:

[player addObserver:self 
         forKeyPath:@"currentItem.status"
            options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
            context:nil];

然后添加一个像这样的观察者方法:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    if ([object isKindOfClass:[AVPlayerItem class]] && [keyPath isEqualToString:@"status"]) {
        AVPlayerItem *playerItem = (AVPlayerItem *)object;
        NSLog(@"avplayer: %@ status: %d", playerItem, playerItem.status);
    }
    else if ([object isKindOfClass:[AVPlayer class]] && [keyPath isEqualToString:@"currentItem.status"]) {
        AVPlayer *player = (AVPlayer *)object;
        NSLog(@"avplayer: %@ status: %d", player.currentItem, player.currentItem.status);
    }
}

如果您仍然没有准备好播放器项目,请确保音频文件确实存在于URL指定的文档目录路径中。