Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer doesn't play more than one time

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?

like image 804
Max Lukin Avatar asked Oct 31 '12 07:10

Max Lukin


2 Answers

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];
like image 92
IanStallings Avatar answered Sep 28 '22 08:09

IanStallings


In Swift 4

You can use below line to play again.

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

Happy to help :)

like image 31
Nazrul Islam Avatar answered Sep 28 '22 09:09

Nazrul Islam