美文网首页
视频播放

视频播放

作者: C_G__ | 来源:发表于2019-04-26 17:27 被阅读0次

VideoView播放器

示例代码


申请权限
AndroidManifest.xml

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

activity_video_player.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chapter08.VideoPlayerActivity"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_vp_play"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="播放"
                    android:layout_weight="1"/>
                <Button
                    android:id="@+id/btn_vp_pause"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="暂停"
                    android:layout_weight="1"/>

                <Button
                    android:id="@+id/btn_vp_replay"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="重播"
                    android:layout_weight="1"/>
            </LinearLayout>
            <VideoView
                android:id="@+id/vv_vp_1"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

VideoPlayerActivity.java

public class VideoPlayerActivity extends AppCompatActivity {

    private VideoView mVideoView;

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

        mVideoView = findViewById(R.id.vv_vp_1);

        // 播放
        Button btn_vp_play = findViewById(R.id.btn_vp_play);
        btn_vp_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mVideoView.isPlaying()) {
                    mVideoView.start();
                }
            }
        });

        // 暂停
        Button btn_vp_pause = findViewById(R.id.btn_vp_pause);
        btn_vp_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mVideoView.isPlaying()) {
                    mVideoView.pause();
                }
            }
        });

        // 停止
        Button btn_vp_replay = findViewById(R.id.btn_vp_replay);
        btn_vp_replay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mVideoView.isPlaying()) {
                    mVideoView.resume();
                }
            }
        });

        // 请求权限
        if (ContextCompat.checkSelfPermission(VideoPlayerActivity.this
                , Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(VideoPlayerActivity.this,
                    new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            initVideoPath();
        }
    }

    private void initVideoPath() {
        try {
            // 创建文件
            File file = new File(Environment.getExternalStorageDirectory(), "test.mp4");
            // 设置路径
            mVideoView.setVideoPath(file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @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_LONG).show();
                    finish();
                }
                break;
            default:
                break;
        }
    }

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


相关文章

  • 初级视频播放功能

    打开相册选择视频 使用系统播放器播放视频 使用VideoView播放视频 使用SurfaceView播放视频 vo...

  • 3.4 音频播放.视频播放.相册调用.相机调用

    音频播放.视频播放.相册调用.相机调用 音频播放 视频播放 相册调用 视频音频资源 视频音频资源.png

  • 视频播放器

    系统播放器 打开视频列表 调用系统播放器播放视频 调用系统播放器播放网络视频 VideoView播放器 调用 V...

  • 视频播放

  • 视频播放

    import "ViewController.h" import "ZSPlayerView.h" @interf...

  • 视频播放

    NSString *file = [[NSBundle mainBundle]pathForResource:@"...

  • 视频播放

    视频播放器的实现有多种方式,此处主要针对主流方式实现一个播放器 MediaPlayer框架

  • 视频播放

    一. 视频播放介绍 实现方案四种: 1.AVPlayer 2.MPMoviePlayerControlle 3.M...

  • 播放视频

    主要使用 VideoView 类来实现。和 MediaPlayer 比较类似。 VideoView工作流程 1. ...

  • 视频播放

    1.依赖compile 'fm.jiecao:jiecaovideoplayer:2.0' compile'fm....

网友评论

      本文标题:视频播放

      本文链接:https://www.haomeiwen.com/subject/thpqnqtx.html