zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    当Django模型迁移时,报No migrations to apply 问题时
    django--各个文件的含义
    django--创建项目
    1013. Battle Over Cities (25)
    1011. World Cup Betting (20)
    1009. Product of Polynomials (25)
    1007. Maximum Subsequence Sum (25)
    1006. Sign In and Sign Out (25)
    1008. Elevator (20)
    1004. Counting Leaves (30)
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781954.html
Copyright © 2011-2022 走看看