• VideoView之视频播放


    运行截图
    这里写图片描述
    AndroidMenifest.xml清单文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="csdn.example.com.notification">
    
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="csdn.example.com.notification.CameraAlbumTest"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
    
            <activity android:name=".PlayVideoTest.Main4Activity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
        </application>
    
    </manifest>

    MainActivity.java

    package csdn.example.com.notification.PlayVideoTest;
    
    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    import java.io.File;
    
    import csdn.example.com.notification.R;
    
    public class Main4Activity extends AppCompatActivity implements View.OnClickListener {
    
        private VideoView videoView;
        Button start, pause, replay;
        TextView time;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main4);
            videoView = (VideoView) findViewById(R.id.video_view);
            start = (Button) findViewById(R.id.start);
            pause = (Button) findViewById(R.id.pause);
            replay = (Button) findViewById(R.id.replay);
            time = (TextView) findViewById(R.id.time);
            start.setOnClickListener(this);
            pause.setOnClickListener(this);
            replay.setOnClickListener(this);
    
            /**
             * 运行时权限处理 ,动态申请WITRE_EXTERNAL_STORAGE权限,由于会在SD卡中放置一个视频文件,程序为了播放这个视频文件
             * 才必须拥有访问SD卡的权限才行
             */
            if (ContextCompat.checkSelfPermission(Main4Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                    PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(Main4Activity.this, new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                }, 1);
            } else {
                initVideoPath();
            }
        }
    
        /**
         * 设置视频路径,会在SD卡的根目录上放置movie.mp4 的视频文件
         */
        private void initVideoPath() {
            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();
                        finish();
                    }
                    break;
                default:
            }
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
    
                case R.id.start:
                    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();//将资源释放掉
            }
        }
    }
    

    activity_main.xml

    <?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/start"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="播放" />
    
            <Button
                android:id="@+id/pause"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="暂停" />
    
            <Button
                android:id="@+id/replay"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="重复" />
        </LinearLayout>
    
        <VideoView
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>
  • 相关阅读:
    VBS处理AD帐号密码到期提醒的脚本[zt]
    简单几步手工扩容LVM、缩小LVM及移除磁盘(笔记)
    python加入进度条:tqdm 和 progressbar
    python的map和reduce函数
    python的lambda表达式
    python的推导式 —— 列表推导式、集合和字典推导式
    Pyspark中遇到的 java.io.IOException: Not a file 和 pyspark.sql.utils.AnalysisException: 'Table or view not found
    pyecharts绘制map地图
    pyecharts绘制geo地图
    sklearn.feature_extraction.text 的TfidfVectorizer函数
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781954.html
Copyright © 2020-2023  润新知