AVPlayer doesn't play more than one time

拜拜、爱过 提交于 2019-12-10 09:43:40

问题


I have some strange problem with playing audio through AVPlayerItem & AVPlayer.

I have recorder and iPod item picker, stream from both of them are going through code like this:

  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
  [audioSession setActive:YES error:nil];

  _playerItem = [[AVPlayerItem alloc] initWithURL:_soundFileURL];
  _player = [[AVPlayer alloc] initWithPlayerItem:_playerItem];
  [_player play];

Everything is actually ok, except one thing, I can't play this stream second or more time. It's just stopped, and no sounds are found after stopping. Does anybody can help with an advice, what the problem could be?


回答1:


You have to set the AVPlayer back to beginning of the stream using prepareToPlay and setting the currentPlaybackTime to 0.

self.avPlayer.currentPlaybackTime = 0;
[self.avPlayer prepareToPlay];



回答2:


In Swift 4

You can use below line to play again.

self.player?.seek(to: CMTime.zero)
self.player?.play()

Happy to help :)



来源:https://stackoverflow.com/questions/13153476/avplayer-doesnt-play-more-than-one-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!