如何在 Android 上使用 VideoView 恢复上次停止的视频? [英] How to resume video where it last stopped using VideoView on Android?

查看:98
本文介绍了如何在 Android 上使用 VideoView 恢复上次停止的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VideoView 制作视频播放器.视频可以播放,但是当我关闭应用程序并重新打开它时,视频不会从停止的地方继续播放,而是从头开始.有谁知道如何解决这一问题?谢谢!

I'm trying to make a video player using VideoView. The video can play, but when I close the app and re-open it, the video doesn't resume from where it stopped and starts from the beginning. Does anyone know how to fix this? Thank you!

这是我正在使用的代码:

This is the code I'm using:

package com.example.praythisworks;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.MediaController;
import android.widget.VideoView;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    static final String STATE_USER = "user";
    private String mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        VideoView videoView =(VideoView)findViewById(R.id.vdVw);
        //Set MediaController  to enable play, pause, forward, etc options.
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
        //Location of Media File
        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.thefeels);
        //Starting VideView By Setting MediaController and URI
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
    }

    
}

推荐答案

您可以通过 videoView.getCurrentPosition();

所以在暂停之前获取视频的当前位置

So before pausing get the current position of the video

int seekPosition = videoView.getCurrentPosition();

在恢复视频时,您可以在 VideoView 上调用 seekTo() 方法并像下面一样启动它.

while resuming the video you can call seekTo() method on VideoView and start it like below.

videoView.seekTo(seekPosition);
videoView.start();

这篇关于如何在 Android 上使用 VideoView 恢复上次停止的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆