尝试播放歌曲文件(通过 MPMediaPickerController 选择)时出现上述错误。在 iOS6 中运行良好,但在 iOS5 上出现错误。这是代码:
DLog(@"song: %@", self.customSongUrl); //always logs a valid url such as ipod-library://item/item.mp3?id=-1890948386979134309
if (self.customSongUrl) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.customSongUrl error:&error];
if (!self.audioPlayer)
{
NSLog(@"AVAudioPlayer could not be established: %@", error); //results in Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
}
[self.audioPlayer prepareToPlay];
[self.audioPlayer setNumberOfLoops:-1];
[self.audioPlayer play];
});
}
根据我所做的研究,错误 -43 表明该文件不存在。但是该文件是从带有 MPMediaPickerController 的 iTunes 库中选择的。再说一次,这个错误只发生在 iOS5 上。那么为什么iOS5会认为这首歌不存在呢?
作为参考,这就是我从媒体选择器获取歌曲网址的方式:
- (void)mediaPicker:(MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *) collection
{
MPMediaItem *item = [[collection items] objectAtIndex:0];
self.customSongUrl = [item valueForProperty:MPMediaItemPropertyAssetURL];
[self dismissViewControllerAnimated:YES completion:NULL];
}