【发布时间】:2014-05-05 12:44:10
【问题描述】:
我有一个适用于 iOS6 及更高版本的音乐播放器应用程序。我的音乐播放器运行良好,它可以在线(流媒体)和离线(本地 mp3 文件)播放音乐。
当我将蓝牙耳机连接到我的设备(iPad 或 iPhone)时,问题出现了,如果设备已解锁,它工作正常,但当设备锁定时,音乐会继续播放,直到歌曲结束,然后当它尝试播放下一首歌曲时歌曲没有播放。 经过一些调试后,我意识到问题出在本地文件上,而且文件似乎不是“可播放”的,但这确实是因为设备解锁后它可以正常工作。
我的代码:
NSURL *URL = [[NSURL alloc] initFileURLWithPath:path];
AVAsset *asset = [AVURLAsset URLAssetWithURL:URL options:nil];
if(asset.isPlayable) {
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:anItem];
if(![self isPlaying]) {
[self play];
}
}
“if(asset.isPlayable) {”中的代码永远不会执行。
更新:
我已使用@MDB983 建议更改了我的代码,但它仍然不起作用。
NSURL *URL = [[NSURL alloc] initFileURLWithPath:path];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithURL:URL];
if(_player == nil) {
_player = [AVPlayer playerWithPlayerItem:anItem];
} else {
[_player replaceCurrentItemWithPlayerItem:anItem];
}
if(![self isPlaying]) {
[self play];
}
有什么想法吗?
【问题讨论】: