简体   繁体   中英

How to get music to stop on next button click

I have some code that starts text to speech and begins playing a sound when a button is pressed. Everything is working fine except for one detail. When two buttons are pressed consecutively, the sounds begin to overlap. Is there a way to cut off the mediaplayer every time a button is pressed? I have used noise1.pause(), noise1.stop(), and noise1.reset() but nothing works. I was thinking about using mediaplayer complete listener but I'm not sure how I could implement this. Any help would be appreciated. Thanks a lot!!!

public void onClick(View view) {

    Resources res = getResources();
    final TextView tv = (TextView) findViewById(R.id.color_text);

    final MediaPlayer noise1 = MediaPlayer.create(this, R.raw.one);
    final MediaPlayer noise2 = MediaPlayer.create(this, R.raw.two);
    final MediaPlayer noise3 = MediaPlayer.create(this, R.raw.three);

    switch (view.getId()) {
        case R.id.one_button:

            String oneString = res.getString(R.string.One);
            tv.setText(oneString);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null);
            } else {
                tts.speak(oneString, TextToSpeech.QUEUE_FLUSH, null, null);
            }

            noise1.start();

            break;

        case R.id.two_button:

            String twoString = res.getString(R.string.Two);
            tv.setText(twoString);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null);
            } else {
                tts.speak(twoString, TextToSpeech.QUEUE_FLUSH, null, null);
            }

            noise2.start();

            break;

        case R.id.three_button:

            String threeString = res.getString(R.string.Three);
            tv.setText(threeString);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null);
            } else {
                tts.speak(threeString, TextToSpeech.QUEUE_FLUSH, null, null);
            }

            three.start();

            break;

}

Define and initialize the MediaPlayer instances outside the onClick(...) method and the stop() method should work.

Here is what I mean:

MediaPlayer noise1, noise2, noise3;
TextView tv;

public void onCreate(....){
    .....
    tv = (TextView) findViewById(R.id.color_text);

    noise1 = MediaPlayer.create(this, R.raw.one);
    noise2 = MediaPlayer.create(this, R.raw.two);
    noise3 = MediaPlayer.create(this, R.raw.three);
    ....
}

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.one_button:
            ...
            noise1.start();
            noise2.stop();
            noise3.stop();
        break;

        case R.id.two_button:
            ...
            noise1.stop();
            noise2.start();
            noise3.stop();
        break;

    case R.id.three_button:
            ...
            noise1.stop();
            noise2.stop();
            noise3.start();
        break;
}

It doesn't work now because you're trying to stop a MediaPlayer instance that you've just created not the one that was created and started on the previous button press.

I don't have reputation for commenting, so have this instead.

The created MediaPlayer objects are within the scope of the method, which means as soon as onClick returns, the objects have no reference, and hence, cannot be stopped.

The solution is to move the objects higher in the scope, either having references in the encompassing object or storing the MediaPlayer s in a collection. Then, at the start of the onClick method, stop the old ones before creating new ones and overriding the reference.

Unless there is some caveat with Android, this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM