赞
踩
继承
java.lang.Object
↳ android.view.View
↳ android.view.SurfaceView
↳ android.widget.VideoView
public class MainActivity extends AppCompatActivity { private VideoView mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initView() { mVideoView = findViewById(R.id.videoView); } private void initData() { //播放本地视频 mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video)); mVideoView.start(); //网络视频 // mVideoView.setVideoPath("http://xx.mp4"); // mVideoView.start(); // 常用方法 // 开始 // mVideoView.start(); // 暂停 // mVideoView.pause(); // 重播 // mVideoView.resume(); // 停止 // mVideoView.stopPlayback(); // 获取文件长度 // mVideoView.getDuration(); // 获取当前播放位置 // mVideoView.getCurrentPosition(); // 判断视频是否播放 // mVideoView.isPlaying(); // 从第几秒开始播放 // mVideoView.seekTo(); // 视频播放完回调 mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { Log.d("videoView", "setOnCompletionListener"); mVideoView.resume(); } }); // 视频播放错误回调 mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { Log.d("videoView", "setOnErrorListener"); return false; } }); // 播放信息回调 mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() { @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { Log.d("videoView", "setOnInfoListener"); return false; } }); //视频加载完成回调 mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { Log.d("videoView", "setOnPreparedListener"); mVideoView.seekTo(mVideoView.getDuration() / 2); } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。