【Android -- 视频】使用 VideoView 播放视频

前言

本篇博客就来讲讲 VideoView 如何播放视频,最后将以一个简单的 Demo 演示。

常用方法

  • int getCurrentPosition():获取当前播放的位置。
  • int getDuration():获取当前播放视频的总长度。
  • isPlaying():当前VideoView是否在播放视频。
  • void pause():暂停
  • void seekTo(int msec):从第几毫秒开始播放。
  • void resume():重新播放。
  • void setVideoPath(String path):以文件路径的方式设置VideoView播放的视频源。
  • void setVideoURI(Uri uri):以Uri的方式设置VideoView播放的视频源,可以是网络Uri或本地Uri。
  • void start():开始播放。
  • void stopPlayback():停止播放。
  • setMediaController(MediaController controller):设置MediaController控制器。
  • setOnCompletionListener(MediaPlayer.onCompletionListener l):监听播放完成的事件。
  • setOnErrorListener(MediaPlayer.OnErrorListener l):监听播放发生错误时候的事件。
  • setOnPreparedListener(MediaPlayer.OnPreparedListener l)::监听视频装载完成的事件。

实例

在这里插入图片描述

1. 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.hjq.bar.TitleBar
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:background="@color/teal_200"
        android:layout_height="?android:attr/actionBarSize"
        app:title="VideoView 使用"
        app:titleStyle="bold"
        app:titleSize="18sp"
        app:backButton="false"
        app:titleColor="@color/white"/>

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="300dp" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始" />

    <Button
        android:id="@+id/btn_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂停 " />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="终止" />


</LinearLayout>

2. 代码

public class MainActivity extends BaseActivity {
    @BindView(R.id.btn_start)
    Button mStart;

    @BindView(R.id.btn_pause)
    Button mPause;

    @BindView(R.id.btn_stop)
    Button mStop;

    @BindView(R.id.videoView)
    VideoView mVideoView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        //根据文件路径播放
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            mVideoView.setVideoPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/activity_local_video.mp4");
        }

        //读取放在raw目录下的文件
        //videoView.setVideoURI(Uri.parse("android.resource://com.jay.videoviewdemo/" + R.raw.lesson));
        mVideoView.setMediaController(new MediaController(this));
    }

    @OnClick({R.id.btn_start,R.id.btn_pause,R.id.btn_stop})
    public void clicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                mVideoView.start();
                break;

            case R.id.btn_pause:
                mVideoView.pause();
                break;

            case R.id.btn_stop:
                mVideoView.stopPlayback();
                break;
        }
    }
}

3. 资源文件位置
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值