使用MediaPlayer同时播放多个音频

时间:2018-05-04 13:59:20

标签: android spinner android-mediaplayer

我遇到了同时玩两个MediaPlayers的问题。

在我的代码中,我制作了一个spinner让用户选择他的第一首歌然后是第二首歌,当用户选择开始播放的第一首歌时以及用户选择第二首歌时它将与第一首歌一起播放。

但问题是,当用户选择第二首歌时,它会停止第一首歌......

我该怎么做才能解决问题?

泰!

代码:

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) {
    }
}

2 个答案:

答案 0 :(得分:0)

尝试SoundPool,它是一种更好的同时处理多个音频文件的方法。

继承文件:

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

以下是示例:

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

希望这有帮助

Reference

答案 1 :(得分:0)

尝试使用两个MediaPlayer个实例。将setSpinner方法更改为:

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) {
        }
    });
}

我不知道这是否会得到您正在寻找的确切行为,但它比使用单个MediaPlayer实例更有可能同时播放两首歌曲。