一、前言
这篇文章主要是学习MediaRecoder的使用,完成视频录制的功能。主要页面布局有三个按钮,点击每个按钮实现不同的功能。
1.主界面布局
<?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:orientation="vertical"
tools:context=".study.media.MediaActivity">
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="录制视频"
android:onClick="record"
android:layout_weight="1"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="145dp" />
<Button
android:id="@+id/button9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放视频"
android:onClick="playVideo"
android:layout_weight="1"
tools:layout_editor_absoluteX="133dp"
tools:layout_editor_absoluteY="284dp" />
<Button
android:id="@+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放音效"
android:onClick="playAudio"
android:layout_weight="1"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="445dp" />
</LinearLayout>
2.主活动
public class MediaActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
//申请音视频的权限
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
}, 100);
}
public void record(View view) {
Intent intent = new Intent(this,MediarRecordActivity.class);
startActivity(intent);
}
public void playVideo(View view) {
Intent intent = new Intent(this,VideoViewActivity.class);
startActivity(intent);
}
public void playAudio(View view) {
Intent intent = new Intent(this,SoundActivity.class);
startActivity(intent);
}
}
二、视频录制
1.音频录制界布局
<?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/texttureView"
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>
2.音频录制活动
public class MediarRecordActivity extends AppCompatActivity implements View.OnClickListener {
private TextureView textureView;//拍摄预览界面
private Button btn_open;//按钮
private MediaRecorder mediaRecorder;
private Camera camera;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
textureView = findViewById(R.id.texttureView);
btn_open = findViewById(R.id.btn_opt);
btn_open.setOnClickListener(this);
}
@Override
public void onClick(View view) {
CharSequence text = btn_open.getText();
if (TextUtils.equals(text, "开始")) {
btn_open.setText("结束");
camera = Camera.open();
//设置相机拍摄时的角度
camera.setDisplayOrientation(90);
camera.unlock();
mediaRecorder = new MediaRecorder();
mediaRecorder.setCamera(camera);
//设置音频源 麦克风
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设备视频源 摄像头
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//指定视频文件格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置音频编码规范
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置拍摄完成的视频的角度
mediaRecorder.setOrientationHint(90);
//设置视频输出文件
mediaRecorder.setOutputFile(new File(getExternalFilesDir(""), "a.Mp4").getAbsolutePath());
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setPreviewDisplay(new Surface(textureView.getSurfaceTexture()));
try {
//开始录制
mediaRecorder.prepare();
} catch (Exception e) {
Log.d("TAG", "录制prepare()异常 " + e.getMessage());
e.printStackTrace();
}
try {
mediaRecorder.start();
} catch (Exception e) {
Log.d("TAG", "录制start()异常 " + e.getMessage());
e.printStackTrace();
}
} else {
btn_open.setText("开始");
mediaRecorder.stop();
mediaRecorder.release();
camera.release();
}
}
}
三、视频播放
1.视频播放界面布局
<?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/texttureView"
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>
2.视频播放活动
public class VideoActivity extends AppCompatActivity implements View.OnClickListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {
private TextureView textureView;//拍摄预览界面
private Button btn_open;//按钮
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
textureView = findViewById(R.id.texttureView);
btn_open = findViewById(R.id.btn_opt);
btn_open.setOnClickListener(this);
}
@Override
public void onClick(View view) {
CharSequence text = btn_open.getText();
if (TextUtils.equals(text, "开始")) {
btn_open.setText("结束");
mediaPlayer = new MediaPlayer();
//设置准备监听
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
try {
//指定视频源
mediaPlayer.setDataSource(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
mediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
mediaPlayer.prepareAsync();
} else {
btn_open.setText("开始");
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
btn_open.setText("开始");
mediaPlayer.release();
}
}
四、使用更简便的方式实现视频播放
1. 使用VideoView实现音频播放界面布局
<?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/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2.使用VideoView实现音频播放的活动
public class VideoViewActivity extends AppCompatActivity implements View.OnClickListener {
//videoView好多控件都是固定的不能改动,如果要定制这个控件就不适合
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoview);
VideoView videoView = findViewById(R.id.video_view);
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 view) {
Log.i("videoView", "===== ");
}
}
3.总结
VideoView中播放控件是固定的,如果要定制还是要使用第一种方式。
五、音频播放
1.音频播放界面布局
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.音频播放活动
public class SoundActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {
private SoundPool soundPool;
public static class Sound{
String name;
int soundId;
public Sound(String name, int soundId) {
this.name = name;
this.soundId = soundId;
}
public String getName() {
return name;
}
public int getSoundId() {
return soundId;
}
}
List<Sound> data;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound);
//给RecycleView设置排列方式
RecyclerView recycleView = findViewById(R.id.recycleView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycleView.setLayoutManager(linearLayoutManager);
soundPool = new SoundPool.Builder().setMaxStreams(6).build();
soundPool.load(this,R.raw.a1,1);
data = new ArrayList<>();
data.add(new Sound("a1", soundPool.load(this,R.raw.a1,1)));
data.add(new Sound("a2", soundPool.load(this,R.raw.a2,1)));
data.add(new Sound("a3", soundPool.load(this,R.raw.a3,1)));
data.add(new Sound("a4", soundPool.load(this,R.raw.a4,1)));
data.add(new Sound("a5", soundPool.load(this,R.raw.a5,1)));
data.add(new Sound("a6", soundPool.load(this,R.raw.a6,1)));
MyAdapter myAdapter = new MyAdapter(data,recycleView, this);
myAdapter.setOnItemClickListener(this);
recycleView.setAdapter(myAdapter);
}
@Override
public void onItemClick(int position) {
Sound sound = data.get(position);
soundPool.play(sound.getSoundId(),
1.0f,1.0f,1,0,1.0f);
}
@Override
protected void onDestroy() {
super.onDestroy();
for (Sound datum :data){
soundPool.unload(datum.getSoundId());
}
soundPool.release();
}
}
3.适配器
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHoder> implements View.OnClickListener {
private final List<SoundActivity.Sound> data;
private final Context content;
private final RecyclerView recycleView;
private OnItemClickListener listener;
public MyAdapter(List<SoundActivity.Sound> data, RecyclerView recyclerView, Context context){
this.data = data;
this.recycleView = recyclerView;
this.content = context;
}
@NonNull
@Override
public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
TextView textView = new TextView(content);
LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = 18;
layoutParams.leftMargin = 18;
textView.setLayoutParams(layoutParams);
textView.setOnClickListener(this);
return new MyViewHoder(textView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHoder holder, int position) {
((TextView)holder.itemView).setText(data.get(position).getName());
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public void onClick(View view) {
if (listener != null){
listener.onItemClick(recycleView.getChildAdapterPosition(view));
}
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
public interface OnItemClickListener{
void onItemClick(int position);
}
public class MyViewHoder extends RecyclerView.ViewHolder {
public MyViewHoder(@NonNull View itemView) {
super(itemView);
}
}
}
4.注意
在实现音频播放功能的时候需要自行下载六个音频到工程raw文件下面分别命名a1,a2,a3,a4,a5,a6.