Android Gapless Audio Looping

This question has been asked several times, but so far I have tried every solution, including custom ones, and the result is not perfect.

I am trying to achieve a continuous carefree audio loop in the application, and so far I have achieved this inconsistently.

I have an application that creates 14 seconds / 14,200 ms audio files. (I myself exported them to .ogg, and the lengths are all the same.

mediaPlayer.setLooping (true); it just doesn’t have trouble-free cycles, regardless of the file format. I tried .mp3, .wav and .ogg for each solution.

The same applies to mediaPlayer1.setOnCompletionListener and alternates between two media planners.

My own solution (which is currently my best):

I calculate how long the file is in milliseconds (14200) and runs runnable, which is delayed to run in 14200 - 30 milliseconds. then play3 runnable will start mediaplayer1 and continue the loop.

corresponding code:

durationHandler.postDelayed(play2, mediaPlayer1.getDuration() - 30); mediaPlayer1.start(); } private Runnable play2 = new Runnable() { public void run() { durationHandler.postDelayed(play3, mediaPlayer2.getDuration() - 30); mediaPlayer2.start(); } }; 

This code really works very well sometimes, but not every time. For example, the first cycle may have a small gap, but the second cycle is great. I guess this has something to do with the actual phone, taking a little longer or less time to process the code. It makes sense?

I tried soundpool and it will not work for 14 second files.

If anyone has any other suggestions for CONSISTENT without spaces in 14 second files, I would like to hear them.

+5
source share

Source: https://habr.com/ru/post/1207151/


All Articles