Alex Atwater
Alex Atwater

Reputation: 150

Play sound every 6-10 seconds

How do you repeat a sound every 6-10 seconds, meaning a random interval between 6 and 10? So the sound plays 6, then 7, 6, 10, etc? So far I have this, which works how I want and plays the sound “once” and changes the button on the screen. However to play the sound again i must click the button to “stop” then click again to “go”. I want to Hit then button and have the sound repeat every 6-10 seconds until you hit the stop button...

public class MainActivity extends ActionBarActivity {

MediaPlayer myMediaPlayer = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final MediaPlayer myMediaPlayer = MediaPlayer.create(this, R.raw.whistle);
    Timer soundTimer = new Timer();

    Random r = new Random();


    //Button related to play btn
    final Button startStopButton = (Button) findViewById(R.id.startstopbutton);
    startStopButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (startStopButton.getText().equals("Start")) {
                //start the sound


                //change button color and text
                startStopButton.setText("Stop");
                startStopButton.setBackgroundColor(Color.RED);

                myMediaPlayer.start();



            } else  {
                //stop whistle

                //change button color and text
                startStopButton.setText("Start");
                startStopButton.setBackgroundColor(Color.GREEN);




            }


        }
    });

}

Upvotes: 1

Views: 2181

Answers (2)

Dpk
Dpk

Reputation: 199

You can use MediaPlayer.OnCompletionListener to accomplish this, whenever the playback completes get a random number between 6 and 10 and post a runnable to start playback again after the calculated random time.

Here is an example of doing this.

public class MainActivity extends Activity {
Button button1;
MediaPlayer mediaPlayer = null;
private final Random mRandom = new Random();
int delayMillis;
Handler handler;
Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.button1);
    handler = new Handler();
    runnable = new Runnable() {

        @Override
        public void run() {
            mediaPlayer.start();

        }
    };
    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            toggleMediaPlayer();

        }
    });
}

private void toggleMediaPlayer(){
    if(mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer=null;
            handler.removeCallbacks(runnable);
    }else{
        mediaPlayer = MediaPlayer.create(this, R.raw.hangouts_incoming_call);
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();

            }
        });
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                delayMillis = 1000 * mRandom.nextInt(5) + 6;
                handler.postDelayed(runnable,delayMillis);
            }
        });

    }
}}

Hope This helps .

Upvotes: 0

Jared Rummler
Jared Rummler

Reputation: 38131

You can get a random number between 6 and 10 like this:

int max = 10;
int min = 6;
int randomNum = new Random().nextInt((max - min) + 1) + min;
int seconds = randomNum * 1000;

Here is an example to play a sound every 6 to 10 seconds:

private final Random mRandom = new Random();

private final Handler mHandler = new Handler();

private MediaPlayer mPlayer;

private boolean mKeepPlaying = true;

private void playMySound() {
    if (mPlayer == null) {
        mPlayer = MediaPlayer.create(this, R.raw.whistle);
    }
    int delayMillis = 1000 * mRandom.nextInt(5) + 6; // random number between 6 and 10
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (isFinishing()) {
                // Check if the Activity is finishing.
                return;
            }
            mPlayer.start();
            if (mKeepPlaying) {
                // play the sound again in 6 to 10 seconds
                playMySound();
            }
        }
    }, delayMillis);
}

Just call playMySound(); in onCreate(Bundle) and toggle mKeepPlaying when you want to stop.

Upvotes: 1

Related Questions