ドキュメントディレクトリにあるMP3を再生するためにAVPlayerを使用しています(ファイルがそこにあることが確認されています)-AVPlayerItem
待機をロードし、アイテムでAVPlayerItemStatusReadyToPlay
インスタンス化して再生します。AVPlayer
はAVPlayerItemStatusReadyToPlay
呼び出されますが、実際には音声が再生されません。理由は誰にもわかりますか?
- (void)checkFileExists {
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.urlForEightBarAudioFile path]]) {
[self beginEightBarDownload];
} else {
// set up audio
[self setupAudio];
//NSLog(@"file %@ already exists", [self.urlForEightBarAudioFile path]);
}
}
- (void)setupAudio
{
AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
[self.eightBarsPlayerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([object isKindOfClass:[AVPlayerItem class]])
{
AVPlayerItem *item = (AVPlayerItem *)object;
if ([keyPath isEqualToString:@"status"])
{
switch(item.status)
{
case AVPlayerItemStatusFailed:
break;
case AVPlayerItemStatusReadyToPlay:
self.player = [[AVPlayer alloc] initWithPlayerItem:self.eightBarsPlayerItem];
[self.player play];
NSLog(@"player item status is ready to play");
break;
case AVPlayerItemStatusUnknown:
NSLog(@"player item status is unknown");
break;
}
}
else if ([keyPath isEqualToString:@"playbackBufferEmpty"])
{
if (item.playbackBufferEmpty)
{
NSLog(@"player item playback buffer is empty");
}
}
}
}