媒体播放器同时播放多个文件

时间:2013-08-21 16:28:30

标签: android media-player

我遇到一个问题,一旦用户点击我的ListView播放的声音,然后在播放声音时他们点击另一个声音,他们点击的2个声音同时播放。

我想获得当前播放的声音,完成,然后开始他们最近点击的新声音。

如果有人可以帮助我,那将非常感激!

CODE:

ListView BoardList = (ListView) findViewById(R.id.BoardList);

    String List[] = {

     "Audio1", "Audio2", "Audio3", "Audio4", "Audio5"
         , "Audio6", "Audio7", "Audio8", "Audio9"
           , "Audio10", "Audio11", "Audio12" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.listcustomize, R.id.textItem, List);

    BoardList.setAdapter(adapter);

    BoardList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            MediaPlayer mPlayer = null;
            if (position == 0) {



                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio1);
                mPlayer.start();
            }

            if (position == 1) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio2);
                mPlayer.start();

            }
            if (position == 2) {


                mPlayer = MediaPlayer.create(HodgeMain.this, R.raw.Audio3);
                mPlayer.start();

            }
            if (position == 3) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio4);
                mPlayer.start();
            }
            if (position == 4) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio5);
                mPlayer.start();
            }
            if (position == 5) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio6);
                mPlayer.start();
            }
            if (position == 6) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio7);
                mPlayer.start();
            }

            if (position == 7) {


                mPlayer = MediaPlayer
                        .create(HodgeMain.this, R.raw.Audio8);
                mPlayer.start();
            }
            if (position == 8) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio9);
                mPlayer.start();
            }
            if (position == 9) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio10);
                mPlayer.start();
            }
            if (position == 10) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio11);
                mPlayer.start();
            }
            if (position == 11) {

                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio12);
                mPlayer.start();

            }

4 个答案:

答案 0 :(得分:5)

Media player playing multiple files at the same time

<强>尝试

声明所有

共同的MediaPlayer mPlayer;

<强>像

BoardList.setAdapter(adapter);
MediaPlayer mPlayer;

然后使用mPlayer.release();

if (position == 0) {

    if(mPlayer!=null)
    {
        mPlayer.release();
        mPlayer=null;
    }

    mPlayer = MediaPlayer.create(HodgeMain.this,
            R.raw.Audio1);
    mPlayer.start();

}
.
.
.
.
if(position==N)
{
        if(mPlayer!=null)
        {
            mPlayer.release();
            mPlayer=null;
        }
        mPlayer = MediaPlayer.create(HodgeMain.this,
                R.raw.AudioN);
        mPlayer.start();
}

关于release():

Releases resources与此MediaPlayer object相关联。

使用MediaPlayer完成后,调用此方法被认为是一种好习惯。

特别是,只要Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called),此方法应为invoked to release the MediaPlayer object,除非应用程序特别需要保留对象。

PS。我尝试使用release()正常工作!

示例:哪个适用于我

try {

if (position == 1) {
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
    mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all);
    mPlayer.start();
}
if (position == 2) {
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
    mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all2);
    mPlayer.start();
}
}

catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}

答案 1 :(得分:0)

在开始之前,请考虑执行以下操作:

if(mPlayer.isPlaying()) mPlayer.stop();
mPlayer.start();

if语句中实现它,看看它是否适合你!

但要实现这一点,您必须在更广泛的范围内定义MediaPlayer对象,而不是每次都声明新的MediaPlayer对象。

答案 2 :(得分:0)

如前所述,您需要使用MediaPlayer来实例化new类,以便在您的类中的任何位置访问其对象,而不是像现在这样静态地访问它。我非常确定咨询此文档可以帮助您了解有关MediaPlayerhttp://developer.android.com/reference/android/media/MediaPlayer.html

的更多信息

答案 3 :(得分:0)

问题是静态方法MediaPlayer.create();实际上是为你返回一个新的MediaPlayer实例。

播放多个音频的原因是因为您没有停止播放先前播放的音频文件(正如其他人建议的那样)。

在调用MediaPlayer.create()之前立即调用此方法:

private void killSounds(){
    try{
        mPlayer.stop();
        mPlayer.release();
    }
    catch(Exception e) {
        //Eat it and do nothing because it's just going to be an NPE when mPlayer is null, which doesn't matter because we're handling it
    }
}

停止上一个MediaPlayer后,你应该可以在没有声音重叠的情况下创建新的MediaPlayer。