刚刚发现一个很奇怪的问题,
从本地存储加载 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