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] 录音与播放录音实现
  3. Android——简单音乐播放器
  4. Android多媒体播放器源码解析(stagefright框架)
  5. Android使用FFmpeg(七)--ffmpeg实现暂停、快退快进播放
  6. Android中自定义SeekBar来控制音量,并与系统音量键的操作保持同步
  7. Android带播放进度条的音乐播放器
  8. Android(安卓)实现气泡布局/弹窗,可控制气泡尖角方向及偏移量
  9. 2011.07.18(4)——— android 播放gif

随机推荐

  1. Android再按一次退出应用
  2. [置顶] android怎样调用@hide和internal
  3. Android架构组件(一):Lifecycle
  4. android Gallery镜像倒影特效另一种方法
  5. Android虚拟键(NavigationBar)适配
  6. Android中使用GSON包解析JSON数据(复杂JSO
  7. Android:从小白到大神_基础普及
  8. Android(安卓)Gallery 3D 特效精华
  9. Gradle使用详解
  10. Android安全性和权限之入门篇