AVPlayer无法在文档目录iOS中播放MP3音频文件 [英] AVPlayer not playing MP3 audio file in documents directory iOS

查看:170
本文介绍了AVPlayer无法在文档目录iOS中播放MP3音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVPlayer播放位于文档目录中的MP3(已确认文件位于其中)-我加载AVPlayerItem等待AVPlayerItemStatusReadyToPlay,然后实例化AVPlayer并播放.

I'm using AVPlayer to play a MP3 located in the documents directory (file is confirmed to be in there) - I load the AVPlayerItem wait for the AVPlayerItemStatusReadyToPlay then instantiate the AVPlayer with the item and play.

AVPlayerItemStatusReadyToPlay确实会被呼叫,但实际上没有音频播放,有人知道为什么吗?

The AVPlayerItemStatusReadyToPlay does get called, but no audio actually plays, anyone have an idea why?

- (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");
            }
        }
    }
}

推荐答案

执行以下操作:

- (void)setupAudio
{
 if([NSFileManager defaultManager] fileExistsAtPath:[self.urlForEightBarAudioFile absoluteString]])
 {
   AVAsset *eightBarAsset = [AVAsset assetWithURL:self.urlForEightBarAudioFile];
   self.eightBarsPlayerItem = [[AVPlayerItem alloc] initWithAsset:eightBarAsset];
   self.eightBarsPlayer = [AVPlayer playerWithPlayerItem:self.eightBarsPlayerItem]; //forgot this line
   [self.eightBarsPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; // add observer for player not item
 }
}

引用 avplayer-and-local-files 链接

这篇关于AVPlayer无法在文档目录iOS中播放MP3音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆