1.MediaPlayer简介
MediaPlayer 类是媒体框架最重要的组成部分之一。此类的对象能够获取、解码以及播放音频和视频,而且只需极少量
设置。它支持多种不同的媒体源,例如:
- 本地资源
- 内部 URI,例如您可能从内容解析器那获取的 URI
- 外部网址(流式传输)
媒体格式列表
https://developer.android.google.cn/guide/topics/media/media-formats?hl=zh_cn
2.MediaPlayer的使用
开始播放:
或者:
代码示例:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
实现代码:
public class VideoActivity extends AppCompatActivity implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
private TextureView textureView;
private Button btn_opt;
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
textureView = findViewById(R.id.textureView);
btn_opt = findViewById(R.id.btn_opt);
btn_opt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
CharSequence text = btn_opt.getText();
if (TextUtils.equals(text, "开始")) {
btn_opt.setText("结束");
mediaPlayer = new MediaPlayer();
//设置准备监听
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
try {
//指定视频源
mediaPlayer.setDataSource(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
mediaPlayer.prepareAsync();
} else {
btn_opt.setText("开始");
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
@Override
public void onCompletion(MediaPlayer mp) {
btn_opt.setText("开始");
mediaPlayer.release();
}
}
3.VideoView
VideoView封装了MediaRecorder,使用较为简便,提供控制窗口:
代码示例:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
实现代码(非常简洁!!!):
public class VideoViewActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoview);
VideoView videoView = findViewById(R.id.videoView);
MediaController mediaController = new MediaController(this);
//点击上一个视频和下一个视频的监听
mediaController.setPrevNextListeners(this, this);
videoView.setMediaController(mediaController);
videoView.setVideoPath(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
videoView.start();
}
@Override
public void onClick(View v) {
Log.i("VideoView","====");
}
}