每6-10秒播放一次声音

时间:2014-12-30 03:08:26

标签: java android

你如何每6-10秒重复一次声音,这意味着一个6到10之间的随机间隔?那么声音会播放6,然后是7,6,10等?到目前为止,我有这个,它按照我想要的方式工作并播放声音“一次”并更改屏幕上的按钮。但是要再次播放声音,我必须单击按钮“停止”然后再次单击“开始”。我想按下然后按钮并让声音每6-10秒重复一次,直到你按下停止按钮......

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);




            }


        }
    });

}

2 个答案:

答案 0 :(得分:1)

您可以像这样获得6到10之间的随机数:

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

以下是每6到10秒播放声音的示例:

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);
}

只需在playMySound();中拨打onCreate(Bundle),然后在您想要停止时切换mKeepPlaying

答案 1 :(得分:0)

您可以使用MediaPlayer.OnCompletionListener来完成此操作,每当播放完成时,获取一个介于6和10之间的随机数,并发布一个runnable,以便在计算出的随机时间后再次开始播放。

这是一个这样做的例子。

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);
            }
        });

    }
}}

希望这有帮助。