6

我遇到了一个问题,一旦用户单击要从我的 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();

            }
4

4 回答 4

6
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();
}
于 2013-08-21T16:35:12.437 回答
0

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

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

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

但要使其工作,您必须MediaPlayer在更广泛的范围内定义您的对象,而不是每次都声明一个新MediaPlayer对象。

于 2013-08-21T16:35:01.153 回答
0

如前所述,您需要实例化MediaPlayer该类,new以便在您的类中的任何位置访问其对象,而不是像现在这样静态地访问它。我很确定查阅本文档将帮助您了解更多关于:http MediaPlayer: //developer.android.com/reference/android/media/MediaPlayer.html

于 2013-08-21T16:38:55.130 回答
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。

于 2013-08-21T20:41:09.420 回答