简体   繁体   中英

How to stop mediaPlayer when back button is pressed or other audio button is pressed?

I'm having a problem with android programming. I want to tell you about the problem. I have a few buttons. The player is running when we click on them. But the sound does not play after the previous sound has ended. Also, when we press the back button, it waits for the sound to end and then performs the function of the back button.

I added onBackPressed and onPause. But the program failed after the first audio play.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class officeSounds extends AppCompatActivity implements View.OnClickListener{

    ImageButton sofitukker,millieturner,jacobbanks,jameshersey;
    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //fullscreen - start
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //fullscreen - end
        setContentView(R.layout.activity_office_sounds);

        sofitukker=findViewById(R.id.sofitukker);
        sofitukker.setOnClickListener(this);
        millieturner=findViewById(R.id.millieturner);
        millieturner.setOnClickListener(this);
        jacobbanks=findViewById(R.id.jacobbanks);
        jacobbanks.setOnClickListener(this);
        jameshersey=findViewById(R.id.jameshersey);
        jameshersey.setOnClickListener(this);
    }
    public void onClick(View view){
        switch(view.getId()){
            case R.id.sofitukker:
                mp = MediaPlayer.create(this, R.raw.sofitukkersound);
                break;
            case R.id.millieturner:
                mp = MediaPlayer.create(this, R.raw.millieturnersound);
                break;
            case R.id.jacobbanks:
                mp = MediaPlayer.create(this, R.raw.jacobbankssound);
                break;
            case R.id.jameshersey:
                mp = MediaPlayer.create(this, R.raw.jamesherseysound);
                break;
            default:
                return;
        }

        mp.start();
        while (mp.isPlaying()) {
        }
        mp.release();

    }
}

I want to stop the previous sound when the back button and a different sound are switched.

Edit: When I added switch-case statement to while (mp.isPlaying()) { //here }

The application does not expect the sound to end. Combines sounds. When the second sound is pressed after the first sound is pressed, the second sound is played before pressing the first sound. So the two sounds are playing together for a while.

You could try adding mp.release() each time you click on screen, so the playing media is automatically released:

public void onClick(View view){
    mp.release();
    switch(view.getId()){
        case R.id.sofitukker:
            mp = MediaPlayer.create(this, R.raw.sofitukkersound);
            break;
        case R.id.millieturner:
            mp = MediaPlayer.create(this, R.raw.millieturnersound);
            break;
        case R.id.jacobbanks:
            mp = MediaPlayer.create(this, R.raw.jacobbankssound);
            break;
        case R.id.jameshersey:
            mp = MediaPlayer.create(this, R.raw.jamesherseysound);
            break;
        default:
            return;
    }

And add this on back pressed method (or prefer mp.stop() if you don't need to keep mp ):

@Override
public void onBackPressed() {
mp.release();
}

MediaPlayer Android

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