仅将视频加载到VideoView中而不立即播放

kiv*_*ivy 4 video android android-videoview

我有一个问题是Android可以将视频数据加载到VideoView没有它立即开始播放的情况下吗?如果是这样,我怎么能这样做?

Kai*_*Kai 15

正如斯格尔所说的那样,我也在考虑展示一个蠢货.但我认为这可能会导致性能下降.

所以我现在正在做的只是打电话

videoView.seekTo(1);
Run Code Online (Sandbox Code Playgroud)

将视频转发到第一毫秒.这对我来说很好,并且很快实现.

  • 这就是钱!:) (2认同)

sgh*_*ael 6

我还希望我的VideoView在活动开始时处于暂停状态.我找不到一种简单的方法让VideoView显示视频的第一个(非黑色)帧.

作为一种解决方法,我从视频资源创建了一个位图缩略图,然后将该缩略图作为背景放置到活动开始的VideoView上.然后,当视频开始播放时,您只需要将背景置零(否则您的播放视频将隐藏在背景图像后面).

Bitmap thumbAsBitmap = null;
BitmapDrawable thumbAsDrawable = null;
private String current;

private void setupVideo() {
    try {

        setVideoFilePath();

        if (current == null || current.length() == 0) {
            Toast.makeText(PreviewMessage.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();
        } else {
            keepScreenOn();

            mVideoView.setVideoPath(getDataSource(current));
            mVideoView.setOnCompletionListener(this);

            // create and place a thumbnail for the start state
            thumbAsBitmap = ThumbnailUtils.createVideoThumbnail(current, MediaStore.Images.Thumbnails.MINI_KIND);
            thumbAsDrawable = new BitmapDrawable(thumbAsBitmap);
            mVideoView.setBackgroundDrawable(thumbAsDrawable);

            mVideoView.pause();
            isPlaying = false;
        }

    } catch (Exception e) {
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

然后,无论你的播放按钮是什么,你都可以做类似的事情

    mPlay.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if(!isPlaying){
                keepScreenOn();
                mPlay.setText("Pause");

                // make sure to null the background so you can see your video play
                mVideoView.setBackgroundDrawable(null);

                mVideoView.start();
                isPlaying = true;
            } else {
                mPlay.setText("Play");
                mVideoView.pause();
                isPlaying = false;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)


Com*_*are 4

VideoView不会自动开始播放,至少如果您有MediaController附件的话。您需要调用start()它才能开始播放。因此,如果您不希望它开始播放,请不要调用start()