7

刚刚发现一个很奇怪的问题,

从本地存储加载 mp3 文件时,

avplayitem 总是处于未知状态,如果文件放在文件夹中,则无法播放。

方式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 &document 中的文件是同一个文件,它可以在任何其他软件中播放,

通过使用方式 2,捆绑文件和文档文件之间的唯一区别是,

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

NSArray *metadata = [playerItem.asset commonMetadata];

但使用文档 url 返回 nil

4

1 回答 1

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 指定的文档目录路径中。

于 2015-03-19T10:07:35.130 回答