简体   繁体   中英

Playing multiple audios simultaneously with MediaPlayer

I got a problem with playing two MediaPlayers simultaneously.

In my code I made a spinner to let the user to choose his first song and then the second song, at the moment that the user chooses the first song it starts to play and when the user choose the second song it will play with the first one.

But the problem is that, when the user chse the second song it stops the first one...

What can I do to fix it?

Ty!

The code:

public void setSpinner() {
    Spinner spine = (Spinner) findViewById(R.id.spinner3);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.song, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spine.setAdapter(adapter);
    final MediaPlayer mp1 = new MediaPlayer();
    spine.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String st = parent.getItemAtPosition(position).toString();
            if (st.equals("First Song")) {
                setMedia1(mp1);
                mp1.start();
            }
            if (st.equals("Second Song")) {
                setMedia2(mp1);
                mp1.start();
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}
public void setMedia1(MediaPlayer mp2) {

    Bundle b1=getIntent().getExtras();
    float s = b1.getFloat("speed");
    Bundle b2=getIntent().getExtras();
    float v = b2.getFloat("volume");

    try {
        String s1 = getIntent().getStringExtra("song1");
        final Uri uri1 = Uri.parse(s1);
        mp2.setDataSource(getApplicationContext(), uri1);
        mp2.prepare();
        mp2.setVolume(v,v);
        mp2.setPlaybackParams(mp2.getPlaybackParams().setSpeed(s));
    } catch (Exception e) {
    }

}
public void setMedia2(MediaPlayer mp2) {

    Bundle b3 = getIntent().getExtras();
    float sp1 = b3.getFloat("speed1");
    Bundle b4 = getIntent().getExtras();
    float vo1 = b4.getFloat("volume1");

    try {
        String s2 = getIntent().getStringExtra("song3");
        final Uri uri2 = Uri.parse(s2);
        mp2.setDataSource(getApplicationContext(), uri2);
        mp2.prepare();
        mp2.setVolume(vo1, vo1);
        mp2.setPlaybackParams(mp2.getPlaybackParams().setSpeed(sp1));
    } catch (Exception e) {
    }
}

try SoundPool,its a better method of dealing with multiple audio files at the same time.

Heres the documentation:

http://developer.android.com/reference/android/media/SoundPool.html

Heres the example:

http://examples.javacodegeeks.com/android/android-soundpool-example

Hope this helps

Reference

Try using two MediaPlayer instances. Change your setSpinner method to do something like:

public void setSpinner() {
    Spinner spine = (Spinner)findViewById(R.id.spinner3);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.song, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spine.setAdapter(adapter);
    // You were only creating a single MediaPlayer instance in your version...
    final MediaPlayer mpA = new MediaPlayer();
    final MediaPlayer mpB = new MediaPlayer();
    spine.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String st = parent.getItemAtPosition(position).toString();
            if (st.equals("First Song")) {
                // Use mpA for the first song
                setMedia1(mpA);
                mpA.start();
            }
            else if (st.equals("Second Song")) {
                // Use mpB for the second song
                setMedia2(mpB);
                mpB.start();
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

I do not know if that will get the exact behavior you are looking for, but it stands a better chance at playing two songs simultaneously than using a single MediaPlayer instance.

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