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

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

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

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

码:

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();

            }
最佳答案
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():

释放与此MediaPlayer对象关联的资源.

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

特别是,每当应用程序的Activity暂停(调用其onPause()方法)或停止(调用其onStop()方法)时,应调用此方法以释放MediaPlayer对象,除非应用程序具有特殊功能需要保持对象周围.

PS.我试过发布()工作正常!

示例:哪个适用于我

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

转载注明原文:android – 媒体播放器同时播放多个文件 - 代码日志