Android Question Gapless Playback with android MediaPlayer

gpapageor

New Member
Licensed User
Hi,
I'm trying to repeatedly play an audio continuously, without any gap but i can't using the MediaPlayer object. It always gives a gap between repeat times. I have tried several audio formats (mp3, wav, ogg) but I constantly get this gap. Is there any other way in B4X environment to achieve this? Has anybody developed a kind of library that handles this situation?

Thanks
 

gpapageor

New Member
Licensed User
Hi Erel,
I am a newcomer in the B4X environment, and I am possibly missing something regarding the internals of MediaPlayer. Actually, my intention is to build an application composed of a set of buttons. Once the user clicks on a button, a predefined music file will be played repeatedly. I have already prepared a music file for that which has a duration of 5 secs, but when it comes to be repeatly executed by the mediaplayer, I've noticed a very short gap in the music during the loop of execution. I've tried to process the audio file with Audicity just to make the ending frequency of the file to follow its starting frequency but I've noticed that the gap although shorter it still exists. So, I was wondering if there is any kind of technique to use two MediaPlayer objects where the second one will start playing the file before the first one finishes the execution or any other workaround that will make the transition of looping cycles more smooth. Attached you can find my code.

Thanks
 

Attachments

  • B4XAudioPlayer.txt
    3 KB · Views: 347
Upvote 0

klaus

Expert
Licensed User
Longtime User
When you post a project you should use Expert As Zip in the File menu of the IDE.
That way we could directly try your program.



I had a look at your code and I think that the problem is here:
B4X:
Sub mp_Complete
    MediaPlayer1.Play
End Sub
When the MediaPlayer finishes playing you start it again.
When you first call Looping_CheckedChange(Looping.Checked) what value does Looping.Checked have.
It should be True.
Make sure that MediaPlayer1.Looping is set to True and comment MediaPlayer1.Play in Sub mp_Complete.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User

I had this problem with both the MediaPlayer and the MediaPlayerStream objects.
The way I resolved it was to create two objects. Load the second one while the first one is still playing, and start playing the second one before the first one finishes - I use an 800ms overlap. It means you have to track progress, but I did get it to work. I don't think posting my code here would help, there are other factors in my application that make it complicated, but the technique itself worked.

In your case you would load the same track into both "players". Since the time taken to load is part of the problem, you could try preloading the second player and use the MP_Complete event of the first one to start the second one playing, but in my experience, you will still have a very small gap this way. It may depend on the track.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Yes, there's a gap and you can't remove it. To ensure a continuous play in games or audio apps, people use other libraries not available in this forum.
Your trick is better than nothing but still unreliable (after a certain number of loops, you'll face errors on some devices as you can read it here).
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If it's just a short audio clip, then Soundpool could do the trick.
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
You'll face errors on some devices as you can read it here).
It actually looks as though some people have been successful with this approach. Until now I've never run extended tests with a loop, and in my app the loop was designed for multiple tracks not a single one. However, can you upload or email me a track you are having trouble with? You said it was a short track so size not a problem.
I'd like to see if there are problems with it in my approach.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…