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

时间:2012-08-28 08:00:17

标签: objective-c ios avfoundation avplayer

我正在使用AVPlayer播放位于文档目录中的MP3(确认文件在那里) - 我加载AVPlayerItem等待AVPlayerItemStatusReadyToPlay然后实例化{{1}与项目和游戏。

AVPlayer确实被调用,但实际上没有音频播放,任何人都知道为什么?

AVPlayerItemStatusReadyToPlay

3 个答案:

答案 0 :(得分:2)

这样做:

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

答案 1 :(得分:0)

in swift

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

答案 2 :(得分:0)

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

}