android中的循环声音文件?

时间:2014-07-15 14:22:56

标签: android audio android-mediaplayer

此代码在初始循环中正常工作。但是,一旦我再次停止并按下我的循环按钮,文件就会混乱并且无序播放。谁有线索有什么不对?任何帮助将不胜感激。

public class OneExp extends ActionBarActivity {

private ListView lv; 
private ArrayAdapter<String> arrayAdapter;
private MediaPlayer mp;
private MediaPlayer mp2;
int[] myAudio = {R.raw.e_1101, R.raw.e_1102, R.raw.e_1103, R.raw.e_1104, R.raw.e_1105,
        R.raw.e_1106, R.raw.e_1107, R.raw.e_1108, R.raw.e_1109, R.raw.e_1110, R.raw.e_1111};
int mCompleted = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one_exp);

    Button btnLoop = (Button) findViewById(R.id.button1);
    Button btnStop = (Button) findViewById(R.id.button2);

    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mp2 != null) {

                if (mp2.isPlaying()) {
                    mp2.stop();

                }
                mp2.reset();
                mp2.release();
                mp2 = null;
            }
            }
    });

    btnLoop.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View arg0) {

            mp2 = MediaPlayer.create(getBaseContext(), myAudio[0]);
            mp2.setOnCompletionListener(new OnCompletionListener() 
            {
                @Override
                public void onCompletion(MediaPlayer mp2)
                {
                    mCompleted++;
                    mp2.reset();
                    if (mCompleted < myAudio.length) 
                    {
                        try
                        {
                            AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
                            if (afd != null) 
                            {
                                mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                                afd.close();
                                mp2.prepare();
                                mp2.start();
                            }
                        }
                        catch (Exception ex) 
                        {
                           ex.printStackTrace();
                        }

                    } 
                    else if (mCompleted>=myAudio.length) 
                    {
                        mCompleted =0;
                        try
                        {
                            AssetFileDescriptor afd = getResources().openRawResourceFd(myAudio[mCompleted]);
                            if (afd != null) 
                            {
                                mp2.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                                afd.close();
                                mp2.prepare();
                                mp2.start();
                            }
                        }
                        catch (Exception ex) 
                        {
                           ex.printStackTrace();
                        }
                    }
                    else
                    {
                        mCompleted=0;
                         mp2.release();
                         mp2 = null;
                    }

                }
            });


            mp2.start();  

        }

    });

1 个答案:

答案 0 :(得分:0)

查看http://developer.android.com/reference/android/media/MediaPlayer.html#setLooping(boolean

为什么要在使用start(),stop(),pause()和setLooping(true)时轻松使用这么多代码呢?

据我所知,没有必要检查音频的长度。

当您调用pause()以及稍后的start()时,您的音频将继续保持原状。

仅在完成文件后才调用释放/重置。