在 VideoView 中从暂停状态播放视频

Posted

技术标签:

【中文标题】在 VideoView 中从暂停状态播放视频【英文标题】:Playing video from the pause state in VideoView 【发布时间】:2012-09-27 08:33:07 【问题描述】:

对于自定义视频视图,我必须重写 MediaController(更改按钮的设计并连接到按钮暂停/播放选择器)。情况是这样的:将视频暂停并打开应用程序,然后再次恢复,视频再次开始播放,而不是从暂停的点开始播放,因此出现了问题:如何让视频开始播放从那个地方,它停止的地方?我需要在 VideoActivity 中进行哪些更改?

public class VideoActivity extends Activity 

    VideoView videoView;

    VkMediaController mc;

    private static final String CURRENT = "duration";

    private static final String URL = "url";

    private Uri mURI;

    private int mCurrentPosition = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.video_view_player);
        videoView = (VideoView) findViewById(R.id.videoViewplayer);
        mc = new VkMediaController(this);
        mURI = getIntent().getData();
        if (savedInstanceState != null) 
            mURI = Uri.parse(savedInstanceState.getString(URL));
            mCurrentPosition = savedInstanceState.getInt(CURRENT);

        
        videoView.setMediaController(mc);
        videoView.setVideoURI(mURI);
        videoView.requestFocus();
    

    @Override
    protected void onStart() 
        super.onStart();
        videoView.start();
        if (mCurrentPosition != -1) 
            videoView.seekTo(mCurrentPosition);
        
    

    @Override
    public void onSaveInstanceState(Bundle outState) 
        outState.putInt(CURRENT, videoView.getCurrentPosition());
        outState.putString(URL, mURI.toString());
        super.onSaveInstanceState(outState);
    

【问题讨论】:

错误/问题是什么?这段代码有什么问题? 没有错误,但不会从当前位置开始播放,而是从头开始 你检查mCurrentPosition的值了吗? 你检查了 canSeekForward 返回的内容吗? 您检查过 onSaveInstanceState 中 getCurrentPosition 的值吗? 【参考方案1】:

我认为问题是当你在onSaveInstanceState(Bundle outState)内部调用时videoView.getCurrentPosition()总是返回0,因为当时视频已经重置,所以当onPause()时调用videoView.getCurrentPosition(),这将返回你想要的值.

【讨论】:

受保护的无效 onPause() videoView.pause(); videoView.getCurrentPosition();超级.onPause(); 正确吗? 我必须将 mCurrentPosition 保留在 onSaveInstanceState(Bundle outState) 中? in onSaveInstanceState(Bundle outState) 我只保存网址? 使 mCurrentPosition 也是静态的【参考方案2】:

我遇到了同样的问题。我以为我做的一切都是正确的,但它没有用。然后我只是更改了顺序,所以我先调用 VideoView.seekTo() 然后再调用 VideoView.start() 。那行得通。 我也有VideoView.requestFocus(),但我不知道这是否重要。

videoPlayer.requestFocus();
videoPlayer.seekTo(position);
videoPlayer.start();

因此,如果您确定位置变量具有正确的值,那么这可能就是您的答案。 未加载视频时,您可能无法搜索。像这样实现 onPreparedListener:

public class VideoActivity extends Activity implements OnPreparedListener 
    VideoView videoPlayer;
    int position;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    if(savedInstanceState!=null)
    position=savedInstanceState.getInt("pos")
    
    // setting up the videoview
    setContentView(R.layout.activity_video);
    getIntent().getExtras().getString("url");
    Uri videouri = Uri.parse(getIntent().getExtras().getString("url"));
    videoPlayer = (VideoView) findViewById(R.id.videoView);
    videoPlayer.setOnPreparedListener(this);
    videoPlayer.setKeepScreenOn(true);
    videoPlayer.setVideoURI(videouri);

    

    /**
     * Start the plaback when the video is loaded
     * 
     * @param mp
     * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
     */
    public void onPrepared(MediaPlayer mp) 
    //this is a TextView infront of the VideoView which tells the User the Video is loading- hide that
    findViewById(R.id.tv_video_load).setVisibility(View.GONE);

    videoPlayer.requestFocus();
    videoPlayer.seekTo(position)
    videoPlayer.start();


【讨论】:

您在 onCreate() 或 onStart() 中调用这些方法? 哦,你说得对。我已经实现了 OnPreparedListener,这一切都在 onPrepared 方法中。如果视频尚未加载,则无法搜索是有道理的。 如果它不起作用,请尝试实现 OnPreparedListener 并在 onPrepared() 方法中调用它。 onStart() 和 onCreate() 在这种情况下几乎相同,因为当您更改方向时,您的 Activity 会重新创建。我会尽快编辑我的答案。【参考方案3】:

Portrait to LandScape LifeCycle Demo

    VideoView videoView;
    int lastPosition;
    int Video_STATE_POSITION  = 0;

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

        if (savedInstanceState != null)
        
            String message = savedInstanceState.getString("message");
            Toast.makeText(this, message, Toast.LENGTH_LONG).show();

            lastPosition=savedInstanceState.getInt("STATE_POSITION");
            videoView.seekTo(lastPosition);
            videoView.start();
            Log.e("Keshav", "MainActivity onCreate() is called  savedInstanceState  lastPosition->" + lastPosition);
        


  @Override
    public void onSaveInstanceState(Bundle outState)
    
        outState.putString("message", "This is my message to be reloaded");
        outState.putInt("STATE_POSITION", Video_STATE_POSITION);
        super.onSaveInstanceState(outState);
    

 @Override
    protected void onPause() 
        super.onPause();

        Video_STATE_POSITION = videoView.getCurrentPosition();

        Log.e("Keshav", "MainActivity onPause is called  ->" +Video_STATE_POSITION);
    

【讨论】:

以上是关于在 VideoView 中从暂停状态播放视频的主要内容,如果未能解决你的问题,请参考以下文章

Android VideoView 缓冲

当应用程序快速进入后台时,如何使用 AVLayer 继续将视频作为 VideoView 层播放

安卓开发怎么点击按钮就播放视频videoview

如何检测 VideoView 何时开始播放(Android)?

android平板videoview播放视频,webview播放flash能旋转180吗?

如何启用 VideoView TouchListener Android?