openfrog
openfrog

Reputation: 40735

Is AVAudioPlayer able to loop large audio files without an audible gap?

I have large looping background music files of up to 10 minutes length. The sounds are looped perfectly and if the player introduces no delay you would not notice where the loop point is.

Can AVAudioPlayer play them without that the user will hear a gap caused by latency or other problems with looping?

Upvotes: 1

Views: 563

Answers (3)

AcmeCat
AcmeCat

Reputation: 224

I had problems trying to create seamless loops. I tried using the numberOfLoops property, with no success. I tried switching to uncompressed audio files, with no success. I even tried switching to an AVAudioQueuePlayer, queued with multiple instances of the same asset, with no success. In the end I solved the problem by creating two instances of AVAudioPlayer (player1 & player2) and switching between them. I began playback of player1 and used player2's prepareToPlay method, and the play(atTime time:) method setting the TimeInterval to player1.deviceCurrentTime - player1.currentTime + player1.duration. I then used the delegate method audioPlayerDidFinishPlaying to prepare player1 to restart playback after player2 was done. To be honest, I don't know why this approach worked and simply setting numberOfLoops to -1 did not. But it may work for you also

Upvotes: 2

sam
sam

Reputation: 3489

You might be hearing a delay because your music is compressed (mp3 for example). AVAudioPlayer has to decode the mp3 as it streams. If you had the same music in an uncompressed format, it would take significantly more disk space, but would loop seamlessly.

Upvotes: 4

Rad'Val
Rad'Val

Reputation: 9231

As long as you don't stop/start it at the end and you set numberOfLoops to a negative integer, it should work.

Upvotes: 2

Related Questions