3

I have started a android.media.MediaPlayer file with:

mp1.start()

and then trying the looping with:

setLooping(true);

but this is ending up with a delay in playing the file again.

I am trying to run an mp3 file containing a rhythm with a set tempo. Is there any better way of looping it in such a manner that the tempo timing does not get disturbed and the rhythm plays seamlessly without any stutter/delay?

Should I use SoundPool instead?

Sid
  • 1,270
  • 2
  • 15
  • 30

5 Answers5

0

Most of best practices for this particular case recommend using .ogg format. You can convert you file easily using VNC media player.

Wiki for .ogg file format - http://en.wikipedia.org/wiki/.ogg

Another solution is the SoundPool and the third one - is to use Audacity and cut the quiet/“blanksound” from you audio file.

Alin Stelian
  • 861
  • 1
  • 6
  • 16
0

Mediaplayer solutions:

If you insist on using MediaPlayer, then you can:

  1. either crop the sound at the end of your audio files, so there's no sound gap between two playback loops

  2. or create a custom solution yourself as the one described here.

Soundpool alternative:

Now, from my personal experience, if you want to loop files small in size and duration, not more than 1MB, then Soundpool is more convenient and it seems that not any relevant problems are reported in contrary to the MediaPlayer. There have been many complaints when trying to loop sounds using MediaPlayer, so generally Soundpool is usually preferred for looping.

Soundpool size limit:

If you are concerned about Sounpool's size limit, keep in mind that it has 1Mb buffer size limit per track. But this limit applies not to file size but to decompressed raw PCM data. SoundPool is going to decompress the loaded audio to PCM data so it is ready to play instantly without the latency of decoding. If the audio you are loading is compressed heavily, such as MP3, then that can get blown up quite a bit.

Improve performance:

Also, as suggested in another answer, files of type ".ogg" according to many sources appear to perform better than ".mp3" in general. So, you should try to convert your files for better performance, but I don't think you will see an improvement concerning looping.

To convert your files you can use an online tool such as this. If you convert your files remember to also make these changes:

  1. Change your sound file's sampling rate to 16000 Hz
  2. Change your audio channel to mono, instead of stereo.
  3. Make sure your file after these processes is smaller than 1 mb in size.
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thanasis M
  • 1,144
  • 7
  • 22
0

If your audio is not long, then use SoundPool for low-latency media playback, instead of MediaPlayer. Also convert it to ogg, as others have already pointed it out.

Edit: if it is just a tempo, and not a continous sound, then maybe you can also measure the latency and seek your audio based on that, but I am not sure you will get better results this way.

keybee
  • 1,498
  • 20
  • 32
0

CODE FOR LOOPING

////////////////////////////////////

             soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);

                int soundID = soundPool.load(OktavaOsnovna.this, raw.looperman, 1);


                soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()

                {
                    public void onLoadComplete(SoundPool soundPool, int sampleId,
                                               int status) {

                        soundPool.play(soundID, 0.5f, 0.5f, 1, -1, 1f);
                    }

                });
                
-1

Please try to do it this way.

audio = MediaPlayer.create(this, R.raw.pl);
audio.setLooping(true);
audio.start();
ZaO Lover
  • 433
  • 2
  • 13