当用户在按下按钮时将活动发送到后台时暂停视频播放

rseugnpd  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(327)

我正在设计一个视频播放器应用程序,当用户通过onbackpressed()或通过intentials按钮离开它使用另一个应用程序并可能稍后回来时,我似乎对该活动有问题。当用户转到另一个活动时,我希望视频播放暂停,当此活动再次获得焦点时,从暂停的位置完全恢复…我尝试用这些方法覆盖,但没有成功,请帮助。。。

public class video_player extends AppCompatActivity {
    VideoView main_screen;
    ListView filelist;
    DrawerLayout mydrawer;
    MediaController mc;
    TextView mytext;
    String video_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Assign value to media controller
         mc=new MediaController(this);
        //Define VideoView
          main_screen = (VideoView) findViewById(R.id.videoplayer);
         Uri uri= Uri.parse(first);
         main_screen.setVideoURI(uri);
         mc.setAnchorView(main_screen);
         mc.setMediaPlayer(main_screen);
         main_screen.setMediaController(mc);
          //Start playback
         main_screen.start();
     }
//method to pause video when user goes to another activity

 protected void onSaveInstanceState(Bundle savedInstanceState){
     //Video can be paused but how do i save this to the outstate Bundle??
        super.onSaveInstanceState(savedInstanceState);
    }
//method to resume playback onrestore but am clueless
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState();
    }
@Override
    protected void onPause(){
        main_screen.pause();
        super.onPause();
    }
    @Override
    protected void onResume(){
        main_screen.resume();
        super.onResume();
    }
}

感谢您花时间阅读此文,非常感谢您的帮助}

ojsjcaue

ojsjcaue1#

在类中声明一个整数,该整数将在onpause()方法中指定视频搜索栏的当前位置,并在onresume()方法中使视频搜索到该整数,如下所示。。。

public class video_player extends AppCompatActivity {
int x; 
 //Then in OnPause and OnCreate
@Override
    protected void onPause(){
        main_screen.pause();
        x=main_screen.getCurrentPosition();
        super.onPause();
    }
    @Override
    protected void onResume(){
        main_screen.resume();
        main_screen.seekTo(x);
        main_screen.start();
        super.onResume();
    }
}

每当活动被发送到后台时,这种方法就起作用

lnxxn5zx

lnxxn5zx2#

try this one 
//when your app is in background   
 protected void onPause(){

    super.onPause();
    main_screen.pause();   
 }

@Override
protected void onResume(){

    super.onResume();
    main_screen.resume();
}

//when your activity is destory 
    @Override
protected void onDestory(){

    super.onDestory();
    main_screen.stop(); //stop or pause what ever 
}

相关问题