简体   繁体   English

如何销毁活动?

[英]How do I destroy an activity?

I have a loop-all button and a stop button. 我有一个全循环按钮和一个停止按钮。 Both buttons work fine while I'm still on the page. 当我仍在页面上时,两个按钮都可以正常工作。 The problem is when I hit the loop-all button, it plays a series of audio files as it is supposed to, but when I leave the page (ie hit the phone's back button) and come back to the page, the audio doesn't stop! 问题是,当我按下“全部循环”按钮时,它会按预期播放一系列音频文件,但是当我离开页面(即按下电话的“后退”按钮)并返回页面时,音频不会别停! I hit the stop button, but it does nothing. 我按下了停止按钮,但是它什么也没做。 The only way to stop it is to go into task manager and end the program. 停止它的唯一方法是进入任务管理器并结束程序。 It seems to me that the reference of mp2 gets lost once I leave the page...Does anyone have any suggestions on how to fix this. 在我看来,一旦离开页面,mp2的引用就会丢失。有人对如何解决此问题有任何建议。 Any help would be appreciated. 任何帮助,将不胜感激。 Here is the code: 这是代码:

                      public class OneVoc extends ActionBarActivity {

private ListView lv;
private MediaPlayer mp;
private MediaPlayer mp2;
int[] myAudio = {R.raw.v_1100, R.raw.v_1101, R.raw.v_1102, R.raw.v_1103, R.raw.v_1104, R.raw.v_1105,
        R.raw.v_1113, R.raw.v_1106, R.raw.v_1107, R.raw.v_1108, R.raw.v_1109, R.raw.v_1110, R.raw.v_1112, 
        R.raw.v_1114, R.raw.v_1115, R.raw.v_1116};
int mCompleted = 0;

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

    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.setOnCompletionListener(null);
                    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();  

        }

    });

try overriding onbackpressed method 尝试覆盖onbackpressed方法

@Override
    public void onBackPressed()
    {
         // code here to stop and finish()
         super.onBackPressed();  
    }

I think it's an It's an Audio buffer issue, memory is loaded in audio buffer and persist later on,finish() might not help u. 我认为这是一个音频缓冲区问题,内存已加载到音频缓冲区中,并在以后持续存在,finish()可能对您没有帮助。 When u press back button on an android button it simply calls the finish() for current activity, while when u press home button it calls onPause(), You need to override onBackPressed() method, and before line super.onbackPressed(); 当您在android按钮上按下后退按钮时,它仅调用当前活动的finish(),而当您按下主屏幕按钮时则调用onPause(),则需要重写onBackPressed()方法,并且在super.onbackPressed()行之前。 Put your code to stop playing. 将您的代码停止播放。 When u go to TakMgr it simply get's the PID refer to ur App process and kills the process. 当您转到TakMgr时,只需获取与您的App进程相关的PID,然后终止该进程即可。 Which is like System.exit(0); 就像System.exit(0); or android.os.process.kill(getMyPid()); 或android.os.process.kill(getMyPid()); Which simply ends ur app which u do not want. 只是结束了您不想要的应用程序。

Can you pause your mp in onPause() and restart it in onResume()? 您可以在onPause()中暂停mp,然后在onResume()中重新启动它吗?
Maybe you want also to store in savedInstance at which point you were! 也许您还想将您当时存储在saveInstance中!

Hope it's helpful 希望对您有所帮助

when you back press the activity just stop the mediaplayer and free the resources and finish the activity.that's it..! 当您向后按下活动时,只需停止媒体播放器并释放资源即可完成活动。就是这样!

@Override
    public void onBackPressed()
    {
         btnStop.performClick();
         super.onBackPressed();  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM