Android 播放视频
2022-02-22 本文已影响0人
gaookey
image.png
使用 VideoView 播放视频
public class MainActivity extends AppCompatActivity {
private VideoView videoView;
private MediaController mController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取界面上的VideoView组件
videoView = findViewById(R.id.video);
// 创建MediaController对象
mController = new MediaController(this);
startVideo();
}
private void startVideo() {
// 设为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video;
videoView.setVideoPath(path); // ①
// 设置videoView与mController建立关联
videoView.setMediaController(mController); // ②
// 设置mController与videoView建立关联
mController.setMediaPlayer(videoView); // ③
// 让VideoView获取焦点
videoView.requestFocus();
videoView.start(); // 开始播放
}
}
使用 MediaPlayer 和 SurfaceView 播放视频
public class MainActivity extends AppCompatActivity {
private SurfaceView surfaceView;
private MediaPlayer mPlayer;
private ImageButton playBn, pauseBn, stopBn;
// 记录当前视频的播放位置
int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建MediaPlayer
mPlayer = new MediaPlayer();
surfaceView = this.findViewById(R.id.surfaceView);
// 设置播放时打开屏幕
surfaceView.getHolder().setKeepScreenOn(true);
surfaceView.getHolder().addCallback(new SurfaceListener());
// 获取界面上的三个按钮
playBn = findViewById(R.id.play);
pauseBn = findViewById(R.id.pause);
stopBn = findViewById(R.id.stop);
config();
}
private void config() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
View.OnClickListener listener = source -> {
int sourceId = source.getId();
if (sourceId == R.id.play) {
playVideo();
} else if (sourceId == R.id.pause) {
if (mPlayer.isPlaying()) {
mPlayer.pause();
} else {
mPlayer.start();
}
} else if (sourceId == R.id.stop) {
if (mPlayer.isPlaying()) {
mPlayer.stop();
}
}
};
// 为三个按钮的单击事件绑定事件监听器
playBn.setOnClickListener(listener);
pauseBn.setOnClickListener(listener);
stopBn.setOnClickListener(listener);
}
private void playVideo() {
mPlayer.reset();
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
mPlayer.setAudioAttributes(audioAttributes);
try {
// 设置需要播放的视频
String path = "android.resource://" + getPackageName() + "/" + R.raw.vvideo;
mPlayer.setDataSource(this, Uri.parse(path));
// 把视频画面输出到SurfaceView
mPlayer.setDisplay(surfaceView.getHolder()); // ①
mPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
// 获取窗口管理器
WindowManager wManager = getWindowManager();
DisplayMetrics metrics = new DisplayMetrics();
// 获取屏幕大小
wManager.getDefaultDisplay().getMetrics(metrics);
// 设置视频保持纵横比缩放到占满整个屏幕
surfaceView.setLayoutParams(new RelativeLayout.LayoutParams(metrics.widthPixels,
mPlayer.getVideoHeight() * metrics.widthPixels / mPlayer.getVideoWidth()));
mPlayer.start();
}
private class SurfaceListener implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (position > 0) {
// 开始播放
playVideo();
// 并直接从指定位置开始播放
mPlayer.seekTo(position);
position = 0;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
// 当其他Activity被打开时,暂停播放
@Override
public void onPause() {
super.onPause();
if (mPlayer.isPlaying()) {
// 保存当前的播放位置
position = mPlayer.getCurrentPosition();
mPlayer.stop();
}
}
@Override
public void onDestroy() {
super.onDestroy();
// 停止播放
if (mPlayer.isPlaying()) mPlayer.stop();
// 释放资源
mPlayer.release();
}
}
摘抄至《疯狂Android讲义(第4版)》