音频循环不会停止......如何破坏循环?

时间:2014-07-16 14:57:35

标签: android audio android-mediaplayer

这是我用来创建循环全部和停止按钮的代码。但是,我有一个错误。如果你点击全部循环按钮就可以正常播放,当你按下停止按钮时,它会停止音频。但是当我点击循环全部按钮时,音频会像往常一样播放,但是当我退出程序(即按下后退按钮)并返回时,停止按钮不会停止音频。为了阻止它,我不得不去任务经理并结束该计划。在这种情况下,这是阻止它的唯一方法。我怎样才能解决这个问题?有人有主意吗?任何帮助将不胜感激。

public class OneGrammar extends ActionBarActivity {

private ListView lv;
private MediaPlayer mp;
private MediaPlayer mp2;
int[] myAudio = {R.raw.g_1100, R.raw.g_1101, R.raw.g_1102, R.raw.g_1103, R.raw.g_1104, R.raw.g_1105,
        R.raw.g_1106, R.raw.g_1107, R.raw.g_1108, R.raw.g_1109, R.raw.g_1110, R.raw.g_1111, R.raw.g_1112, 
        R.raw.g_1113, R.raw.g_1114, R.raw.g_1115};
int mCompleted = 0;

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

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

        }

    });

0 个答案:

没有答案