3

我的活动中有一个 onPause。奇怪的是,在单个活动(videoPlayer)中,onPause 被调用了两次

场景:这发生在我的手机设备上的视频播放器中,而不是平板电脑上。

按电源按钮进入待机状态,调用 onpause 然后调用 onresume 然后再次调用 on pause。然后再次按下电源按钮,并按预期调用恢复。

有没有人遇到过这样的问题,如果有,你是如何解决的。即使只是让 onPause onResume onpause 然后开启 onResume。

提前致谢。这个出现两次

    protected void onPause()
{

    Log.i("VideoPlayer", "onPauseCalled");
    super.onPause();
    pauseMedia();
    Log.i("onPause", " save states to be called");
    if(saveAllowed)
        saveStates();
    Log.i("onPause", " save States called");
    view.setVisibility(View.GONE);
    //Log.i("onPause", "visibility GOne");
    removeListeners();
    doCleanUp();


}

    @Override
protected void onResume()
{
    super.onResume();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    Log.i("VideoPlayer", "onResumeCalled");

    if(pm.isScreenOn())
    {
        //Initialization
        initializeViewControls();
        handler = new Handler();

        initializeButtons();
        initRecordButtons();
        initVolumeControl();

        //RestoreState
        restoreMediaBoolean();
        restoreMediaState();
        Log.i("After", "restoreState");
        view = (SurfaceView) findViewById(R.id.surfaceView);
        //Log.i("After", "GettingView");
        holder = view.getHolder();
        view.setVisibility(View.VISIBLE);
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        Log.i("onResume", "view is Visible");
        saveAllowed = true;
    }
    else
        saveAllowed = false;

}

private void saveStates()
{
    if(!isFinishing)
    {
        //Log.i("VideoPlayer", "Called at saveStates()");

        prefs = getSharedPreferences(PREF_SAVE, Context.MODE_PRIVATE);
        if(prefs != null)
        {
            prefEditor = prefs.edit();
        }

        if(prefEditor != null)
        {

            //Save overlay visibiltiy
            if((volumeLayout != null) && (volumeLayout.getVisibility() == View.VISIBLE))
            {
                prefEditor.putBoolean(IS_VOLUME_VISIBLE, true);
                prefEditor.commit();
            }
            else
            {
                prefEditor.putBoolean(IS_VOLUME_VISIBLE, false);
                prefEditor.commit();
            }

            //Save recorder Overlay state
            if((recordLayout != null) && (recordLayout.getVisibility() == View.VISIBLE) )
            {
                prefEditor.putBoolean(IS_RECORDER_VISIBLE, true);
                prefEditor.commit();
            }
            else
            {
                prefEditor.putBoolean(IS_RECORDER_VISIBLE, false);
                prefEditor.commit();
            }
            //Save controller OVerlay State
            if((backButton != null) && (backButton.getVisibility() == View.VISIBLE))
            {

                prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, true);
                prefEditor.commit();
                Log.i("Save States", "Controller saved as visible");
            }
            else
            {
                Log.i("Save States", "Controller saved as Invisible");
                //Log.i("Saving", "Controller Invisible");
                prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, false);
                prefEditor.commit();
            }

            //Save is private Audio Recorded
            prefEditor.putBoolean(IS_PRIVATE_AUDIO_RECORDED, isCustomVoiceRecorded);
            prefEditor.commit();

            //Save are we playing out custom audio
            prefEditor.putBoolean(PLAY_CUSTOM_AUDIO, playCustomAudio);
            prefEditor.commit();

            //Make boolean is restoring
            prefEditor.putBoolean(IS_RESTORING, true);
            prefEditor.commit();

            //Saves Position of Current Video
            if(vidplayer != null)
            {
                prefEditor.putInt(VIDEO_POSITION, videoPausedAt);
                prefEditor.commit();
            }

            //Save Position of Current Audio
            if(audplayer != null)
            {
                prefEditor.putInt(AUDIO_POSITION, audioPausedAt);
                prefEditor.commit();
            }


            prefEditor = null;
            prefs = null;

        }
    }

发生的事情是我保存了一些布局的状态,比如记录器布局,当它有点像菜单时它会暂停。然后,如果按下电源,它会关闭,但是当它重新打开时,布局消失并且正在播放视频,这是不应该发生的。

4

2 回答 2

3

是的,我遇到了类似的问题。您使用的是哪种设备(如果记忆正确,我认为这是 NFC 屏幕超时问题)?暂停对我来说不是问题,但我尝试在 onResume 中恢复视频(它被调用两次)所以我在 onResume 中使用了以下内容:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
   //now do stuff
}
于 2012-06-06T21:26:34.297 回答
0

尽管这是非常晚的答案,但我只想分享我的经验。我遇到了类似的情况,但这里的主要问题是方向改变,这是由视频播放器(可能是横向播放视频)或您的活动当前处于横向状态引起的。因此,当您按下设备上的电源关闭按钮时,实际生命周期阶段是:onPause()、onStop(),然后更改方向(锁定屏幕默认为纵向),这导致调用 onCreate()、onStart()、onPause( )...

解决方案很简单:通过在清单中的活动定义中指定 android:configChanges="orientation|screenSize" 属性并覆盖 java 类中的 onConfigurationChanged() 来手动处理方向更改

于 2016-05-20T13:52:41.083 回答