Making a sound repeat


Last Updated:

  1. cr5315

    cr5315 Well-Known Member Developer This Topic's Starter

    Joined:
    Jul 23, 2010
    Messages:
    435
    Likes Received:
    158
    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 (Text):
    1. import android.app.Activity;
    2. import android.media.MediaPlayer;
    3. import android.os.Bundle;
    4.  
    5. public class ObeyActivity extends Activity {
    6.     /** Called when the activity is first created. */
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.main);
    11.         final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.laugh);
    12.         mp.start();
    13.     }
    14. }
     

    Advertisement
  2. DominicDroid

    DominicDroid New Member

    Joined:
    Aug 24, 2010
    Messages:
    3
    Likes Received:
    0
    Add the following line of code before you call mp.start(). The sound will repeat until pause or stop is called.

    Code (Text):
    1. mp.setLooping(true);
     

Share This Page

Loading...