简体   繁体   中英

how to pause and resume an audio

I am trying to make a program that plays an audio1 10 times after it plays an audio2 one time and plays the audio1 again 10 times then plays an audio3 one time, the app have a button to pause and resume the current audio and each audio change the imageView , the problem is that everytime I pause the audio1 the audio2 plays too or the current audio stops and plays the next audio. how can I have more control with the audio, I mean I wanna pause the current audio and the other wait until the current audio finish.

public class PrincipalActivity extends ClassePai {

Thread thread;
private int currentPosition, duracaoTotal;

private MediaPlayer avemaria, intro, primeiro, segundo, terceiro, quarto,
        quinto, salve;
private ImageView imagem;
private Button btnProximo, btnAnterior, btnPause;

protected Intent intent;

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

    avemaria = MediaPlayer.create(this, R.raw.teste);

    intro = MediaPlayer.create(this, R.raw.teste);

    primeiro = MediaPlayer.create(this, R.raw.ave_maria);

    segundo = MediaPlayer.create(this, R.raw.ave_maria);

    terceiro = MediaPlayer.create(this, R.raw.terceiro_goz);

    quarto = MediaPlayer.create(this, R.raw.quarto_goz);

    quinto = MediaPlayer.create(this, R.raw.quinto_goz);

    salve = MediaPlayer.create(this, R.raw.salve_bencao);

    imagem = (ImageView) findViewById(id.imageViewGoz);
    imagem.setImageResource(com.willamydotcom.osantoterco.R.drawable.g
            + getContador());

    Thread thread = new Thread() {

        public void run() {

            play(primeiro);

            // wait while the current audio plays
            while (primeiro.isPlaying()) {

            }

            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(avemaria);
            while (avemaria.isPlaying()) {

            }
            play(segundo);
            while (avemaria.isPlaying()) {

            }

        }

    };
    thread.start();

    btnProximo = (Button) findViewById(id.btnTest);

    btnProximo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (avemaria.isPlaying()) {

                avemaria.start();
            } else if (primeiro.isPlaying()) {
                primeiro.start();
            } else if (segundo.isPlaying()) {
                segundo.start();
            }
        }
    });

    btnPause = (Button) findViewById(id.btnPlay);
    btnPause.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (avemaria.isPlaying()) {

                avemaria.pause();
            } else if (primeiro.isPlaying()) {
                primeiro.pause();
            } else if (segundo.isPlaying()) {
                segundo.pause();
            }

        }
    });

    btnAnterior = (Button) findViewById(id.btnAnterior);
    btnAnterior.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_principal, menu);
    return true;
}

public void play(MediaPlayer audio) {
    audio.start();

    audio.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer arg0) {

            imagem.setImageResource(R.drawable.g + getContador());

            setContador(getContador() + 1);

        }
    });

}

}

First, don't forget to call MediaPlayer.prepare on your clips before playing them.

You don't need the while loops after each play() function - in your play function, you are setting an OnCompletionListener - you can put your logic there to decide what to play next when one of your audio clips completes - so it will only play at the end of the clip, not when the clip is paused. Take a look at the functions available in MediaPlayer ( http://developer.android.com/reference/android/media/MediaPlayer.html ), especially setAudioSessionId (you can give each clip an ID so you can tell which clip is complete in your OnCompletionListener)

It may actually be better to just use one MediaPlayer object and change the data source (using setDataSource when necessary...I haven't used this class much, so I'm not sure what is the best way to set it up.

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