File file = new File(Environment.getExternalStorageDirectory(), path);videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
videoView.start(); // 开始播放
videoView.pause(); // 暂停播放
videoView.resume(); // 重新播放
//视频播放public class MainActivity extends AppCompatActivity {    private VideoView videoView;    private Button play;    private Button pause;    private Button replay;    /**     * 设置view播放控制条     */    private MediaController mediaController;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         initView();        initData();    }      private void initView() {        videoView = (VideoView) findViewById(R.id.video_view);        play = (Button) findViewById(R.id.play);        pause = (Button) findViewById(R.id.pause);        replay = (Button) findViewById(R.id.replay); //        //初始化videoview控制条//        mediaController=new MediaController(this);//        //设置videoview的控制条//        videoView.setMediaController(mediaController);//        //设置显示控制条//        mediaController.show(0);    }     private void initData() {        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("123.mp4"); // 初始化MediaPlayer        }        play.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (!videoView.isPlaying()) {                    videoView.start(); // 开始播放                }            }        });        pause.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (videoView.isPlaying()) {                    videoView.pause(); // 暂停播放                }            }        });        replay.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (videoView.isPlaying()) {                    videoView.resume(); // 重新播放                }            }        });    }     private void initVideoPath(String path) {        File file = new File(Environment.getExternalStorageDirectory(), path);        videoView.setVideoPath(file.getPath()); // 指定视频文件的路径    }     @Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {        switch (requestCode) {            case 1:                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                    initVideoPath("movie.mp4");                } else {                    Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();                    finish();                }                break;            default:        }    }     @Override    protected void onDestroy() {        super.onDestroy();        if (videoView != null) {            videoView.suspend();        }    }}

对应xml:

<?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"    android:orientation="vertical"    tools:context=".MainActivity">       <VideoView        android:id="@+id/video_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:id="@+id/ll"        android:layout_alignParentBottom="true"        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="播放" />         <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> </RelativeLayout>

更多相关文章

  1. Android 手机存储文件各种路径
  2. 修改android studio 默认的so文件检索路径
  3. Android获取SD卡路径/内存的几种方法
  4. Android应用程序的数据存放目录 路径
  5. Android获取程序路径 (/data/data/appname)
  6. Android/Bluetooth 初始化流程

随机推荐

  1. 从源代码编译Android(CyanogenMod For He
  2. Android P(api28) 不支持 http 协议解决
  3. android(3)(android五大布局)
  4. Android gradle测试
  5. 分页控件1
  6. Android(安卓)Fragment嵌套导致的bug
  7. Android常用代码
  8. Android拷贝图片到指定文件路径
  9. android 安卓创建文件夹
  10. Android自动补全(二)