• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Making a sound repeat

cr5315

Android Enthusiast
I have an app that plays a sound when it is opened, but I would like it to repeat until the app is closed. I've gotten the sound to play once, but I can't figure out how to make it repeat.

Code:
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;

public class ObeyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.laugh);
        mp.start();
    }
}
 
Add the following line of code before you call mp.start(). The sound will repeat until pause or stop is called.

Code:
mp.setLooping(true);
 
Back
Top Bottom