美文网首页
VideoView,MediaController

VideoView,MediaController

作者: 被罚站的树 | 来源:发表于2019-06-10 13:03 被阅读0次
    image.png
    image.png
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.lindingdu.app02">
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
            tools:ignore="ProtectedPermissions" />
        <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">
            <activity android:name=".MainActivity"
                android:screenOrientation="landscape">
              <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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=".MainActivity">
    
        <VideoView
    
            android:id="@+id/video"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             />
    
    </RelativeLayout>
    
    import android.media.MediaPlayer;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.WindowManager;
    import android.widget.MediaController;
    import android.widget.Toast;
    import android.widget.VideoView;
    
    import java.io.File;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                    ,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
    
            VideoView videoView=findViewById(R.id.video);
    
            /** 加载播放视频 **/
            File file=new File(Environment.getExternalStorageDirectory()+"/a.mp4");
            //获取视频文件对象
            if(file.exists()){
                videoView.setVideoPath(file.getAbsolutePath());//指定要播放的视频
    
            }else{
                Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
            }
            //控制视频播放
            MediaController mc=new MediaController(MainActivity.this);
            videoView.setMediaController(mc);//让VideoView与MediaControl关联
            videoView.requestFocus();//让VideoView获取焦点
            videoView.start();
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Toast.makeText(MainActivity.this, "视频播放完毕", Toast.LENGTH_SHORT).show();
                }
            });
    
        }
    }
    

    相关文章

      网友评论

          本文标题:VideoView,MediaController

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