Riyana
Riyana

Reputation: 241

How do I loop a sound file infinitely in android?

I have done a simple stopwatch app. I have start and pause button to start and stop/pause sound respectively. Right now my buttons are able to start and stop sound. But since my sound file is 2 minutes long, it only plays 2 minutes and after that stops automatically.

Now, what I want is to keep playing the sound forever until the pause button is pressed.

private MediaPlayer mp;//is originally initialized inside the class but outside method
    startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);
           mp.start();
          }
        });

        pauseButton = (Button) findViewById(R.id.pauseButton);

        pauseButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
                mp.stop();

            }
        });

Upvotes: 1

Views: 2417

Answers (2)

Bhaskar
Bhaskar

Reputation: 949

mMediaPlayer.setLooping(true);

Upvotes: 3

ettore
ettore

Reputation: 78

For song looping try using mp.setLooping(true); something like this:

MediaPlayer mp; mp = MediaPlayer.create(this, R.raw.your_song); mp.setLooping(true);

Upvotes: 2

Related Questions