3

I want my program continuously play a particular audio clip without any starting delay for that i've created a program but it is not working as i mensioned so someone please help me to solve this

code

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    LocalService mService;
    boolean mBound = false;
    Button btn1;
    Button btn2;
    MediaPlayer yourStereo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         btn1=(Button)findViewById(R.id.a_button);
         btn2=(Button)findViewById(R.id.b_button);


        btn1.setOnClickListener(this);


    }

    @Override
    public void onClick(View arg0) {
            // play music here
            btn1.setVisibility(Button.GONE);
            btn2.setVisibility(Button.VISIBLE);
            // TODO Auto-generated method stub
            yourStereo = MediaPlayer.create(this, R.raw.pl);
                    yourStereo.start();
                    yourStereo.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            // TODO Auto-generated method stub
                            yourStereo.start();
                        }
                    });
            // pause music here

            btn2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    yourStereo.stop();
                    btn2.setVisibility(Button.GONE);
                    btn1.setVisibility(Button.VISIBLE);

                }
            });

    }
}
Hybrid Developer
  • 2,320
  • 1
  • 34
  • 55

3 Answers3

2

Just setLooping(true) on the MediaPlayer instead of playing it again onCompletion.

http://developer.android.com/reference/android/media/MediaPlayer.html#setLooping(boolean)

Sets the player to be looping or non-looping.

With looping on it'll repeat indefinitely by itself without breaks, you don't have to worry about handling it yourself.

yourStereo = MediaPlayer.create(this, R.raw.pl);
yourStereo.setLooping(true);
yourStereo.start();
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • You sure there is no delay in your file? Like silence at the start or end of it? – Ken Wolf Jun 25 '13 at 09:04
  • You can try using a handler to set seek to beginning x milliseconds before the end of file: http://stackoverflow.com/a/10423155/833647 – Ken Wolf Jun 25 '13 at 09:14
  • 1
    There's indeed issue with setLooping: small delay between end and start. Which is probably due to reloading of file or MediaPlayer seeking to start https://code.google.com/p/android/issues/detail?id=18756 – GregoryK Nov 05 '15 at 07:57
1

Use SoundPool for low-latency media playback, instead of MediaPlayer.

blganesh101
  • 3,647
  • 1
  • 24
  • 44
0

I found other solution:

afd = assetManager.openFd(nameSound);

player.setDataSource(afd.getFileDescriptor(),
                     afd.getStartOffset(), afd.getLength() - 1000);
afd.close();
player.prepare();
player.setLooping(true);
player.start();

You only need to define the duration 1000(0.1ms) less instead that the total duration.

Pang
  • 9,564
  • 146
  • 81
  • 122
yaircarreno
  • 4,002
  • 1
  • 34
  • 32