AVPlayer 不在 iOS 文档目录中播放 MP3 音频文件

Posted

技术标签:

【中文标题】AVPlayer 不在 iOS 文档目录中播放 MP3 音频文件【英文标题】:AVPlayer not playing MP3 audio file in documents directory iOS 【发布时间】:2012-08-28 08:00:16 【问题描述】:

我正在使用 AVPlayer 播放位于文档目录中的 MP3(文件已确认在其中)-我加载 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");
            
        
    

【问题讨论】:

【参考方案1】:

这样做:

- (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链接

【讨论】:

谢谢,但我已经检查了文件是否存在,我已经编辑了我的帖子以显示这一点。 嗯,对了,如果你从本地播放,那么你必须观察播放器,但如果你从远程播放,那么你观察播放器项目?我这样想对吗?你的回答很完美,谢谢。【参考方案2】:

迅速

func setupAudio() 
 if NSFileManager.defaultManager().fileExistsAtPath(self.urlForEightBarAudioFile.absoluteString) 
   self.eightBarsPlayerItem = AVPlayerItem(asset: AVAsset(URL: self.urlForEightBarAudioFile))
   self.eightBarsPlayer = AVPlayer(playerItem: self.eightBarsPlayerItem)
 

【讨论】:

【参考方案3】:
- (void)setupAudio

  dispatch_async(dispatch_get_main_queue(), ^ 
  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 不在 iOS 文档目录中播放 MP3 音频文件的主要内容,如果未能解决你的问题,请参考以下文章

AVPlayer 无法在 iOS8 上播放 NSDocuments 目录中的视频

iOS AVPlayer 无法播放 240 fps 视频

我可以在 iOS 中使用 iPodMusicPlayer 播放文档目录中的歌曲吗?

iOS - AVPlayer 音视频播放

iOS AVPlayer 后台播放问题自动停止问题 防止应用被后台挂起方法

AVPlayerLayer 不在 iOS 11 中渲染离线 HLS 视频