MDP
MDP

Reputation: 4287

My background loop music doesn't "loop" good

I have a background music loop. But it doens't loop well, there's a half second whithout music every time the loop is ending. The mp3 file is perfect, there isn't any second without music. Is it fault of MediaPlayer?

Upvotes: 0

Views: 381

Answers (2)

Michael
Michael

Reputation: 58517

Starting with Jellybean (Android 4.1) you can do "chaining" of MediaPlayers. Before the currently playing MediaPlayer has finished, call setNextMediaPlayer, and the next MediaPlayer will automatically start as soon as possible after the current one has finished in order to avoid gaps.

Note that you're not allowed to set the currently playing MediaPlayer instance as the next one. So in order to use this for looping a song you'd have to create 2 MediaPlayer instances with the same data source and alternate between them.

Upvotes: 1

Paul
Paul

Reputation: 5223

SoundPool does a better job at that.

A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.

Keep in mind that this can be pretty memory intensive depending on how long your music file is.

Upvotes: 0

Related Questions