简体   繁体   中英

How to start a second media player ..?

I am developing an App where I want to play two mp3 files one is background music and another audio plays after 20 seconds.

    mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec); 
    mediaPlayer1 = MediaPlayer.create(this,R.raw.sound3 ); 
    private void buttonClick(){

    if (buttonPlayStop.getText() == getString(R.string.play_str)) {

    buttonPlayStop.setText(getString(R.string.pause_str));

    try
    {
    mediaPlayer.start();
    mediaPlayer1.start();

    startPlayProgressUpdater();
    }
    catch (IllegalStateException e) {
    mediaPlayer.pause();
    }

    } 

使用MediaPlayer播放背景音乐,使用SoundPool播放其他所有内容。

You can play two files in Mediaplayer by using this piece of code in a thread(don't use UI thread for this) :

mediaplayer = new MediaPlayer();

mediaplayer.reset();

//For media file 1

mediaplayer.setDataSource(dataSourceOne);

mediaplayer.prepare();

mediaplayer.start();

Thread.sleep(500);//Set the time as per your need.

//For media file 2

mediaplayer.reset();

mediaplayer.setDataSource(dataSourceTwo);


mediaplayer.setLooping(true);

mediaplayer.prepare();

mediaplayer.start();

As per the better implementation perspective, write an util class with all the common methods (play(),pause(),stop()) and call every method from your class based on the requirement using a thread.

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