在Android中播放视频

本文介绍如何在Android应用中使用VideoView播放本地视频。内容包括在AndroidManifest.xml中添加必要权限,以及在MainActivity布局文件和Java代码中实现播放、暂停和重新播放功能。操作基于手机SD卡根目录下的movie.mp4文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里我是用VideoView来进行播放视频

注意:请事先在手机的SD卡的根目录下放一个名为movie.mp4的视频文件,因为等下我就是读取这个文件的路径并进行播放,暂停和重新播放的操作。

首先我们在AndroidManifest.xml文件中添加以下内容:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

MainActivity的布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/play"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放"
            android:textSize="20sp" />

        <Button
            android:id="@+id/pause"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:textSize="20sp" />

        <Button
            android:id="@+id/replay"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重新播放"
            android:textSize="20sp" />
    </LinearLayout>

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

布局很简单,就是三个按钮,分别控制播放,暂停和重新播放。

MainActivity的java代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_play, btn_pause, btn_replay;
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化组件
        btn_play = findViewById(R.id.play);
        btn_pause = findViewById(R.id.pause);
        btn_replay = findViewById(R.id.replay);
        videoView = findViewById(R.id.video_view);

        // 添加点击事件
        btn_play.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_replay.setOnClickListener(this);

        // 判断是否授权,如果没有,就动态申请
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            initVideoPath();
        }

    }

    private void initVideoPath() {
        // 设置文件为SD卡根目录下的一个为movie.mp4的文件
        File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
        // 指定视频文件的路径
        videoView.setVideoPath(file.getPath());
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initVideoPath();
                } else {
                    Toast.makeText(this, "没有存储权限,请先开启权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play:
                if (!videoView.isPlaying()) {
                    videoView.start();//开始播放
                }
                break;

            case R.id.pause:
                if (videoView.isPlaying()) {
                    videoView.pause(); // 暂停播放
                }
                break;

            case R.id.replay:
                if (videoView.isPlaying()) {
                    videoView.resume();//重新播放
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.suspend();
        }
    }
}

这里除了动态申请权限以外,其它的都见名知义,非常简单,相信你都能看的懂。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值