曲ファイル (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 はその曲が存在しないと考えるのでしょうか?
参考までに、メディア ピッカーから曲の URL を取得する方法を次に示します。
- (void)mediaPicker:(MPMediaPickerController *) mediaPicker didPickMediaItems:(MPMediaItemCollection *) collection
{
MPMediaItem *item = [[collection items] objectAtIndex:0];
self.customSongUrl = [item valueForProperty:MPMediaItemPropertyAssetURL];
[self dismissViewControllerAnimated:YES completion:NULL];
}