Activity代码:
package com.example.videoplaydemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
//定义按钮
private Button reccordBn;
private Button stopBn;
//定义视频文件
private File videoFile;
private MediaRecorder mRecorder;
//显示视频预览
private SurfaceView sView;
//是否在录制视频
private Boolean isRecording;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reccordBn = findViewById(R.id.record);
stopBn = findViewById(R.id.stop);
// 按钮不可用时
stopBn.setEnabled(false);
requestPermissions(new String[]{Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x123);
View.OnClickListener listener = source ->
{
switch (source.getId()) {
// 点击录制按钮
case R.id.record:
if (!Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(MainActivity.this, "SD卡不存在!",
Toast.LENGTH_SHORT).show();
return;
// 创建视频文件
}
videoFile = new File(Environment.getExternalStorageDirectory()
.toString() + "/myVideo.mp4");
mRecorder = new MediaRecorder();
mRecorder.reset();
//采集声音
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置从摄像头采集图像
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//设置文件输出格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置声音编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//设置图像编码格式
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
//设置视频尺寸
//mRecorder.setVideoSize(640, 560);
//每秒12帧
//mRecorder.setVideoFrameRate(12);
mRecorder.setOutputFile(videoFile.getAbsolutePath());
//使用surfaceView来预览视频
mRecorder.setPreviewDisplay(sView.getHolder().getSurface());
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
//开始录制
mRecorder.start();
//录视频的时候,录制按钮不可用,停止按钮可用
reccordBn.setEnabled(false);
stopBn.setEnabled(true);
isRecording = true;
break;
case R.id.stop:
if (isRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
//不录视频时的时候,录制按钮可用,停止按钮不可用
reccordBn.setEnabled(true);
stopBn.setEnabled(false);
}
break;
}
};
//绑定监听
reccordBn.setOnClickListener(listener);
stopBn.setOnClickListener(listener);
//获取界面上的surfaceview
sView = findViewById(R.id.sView);
//设置分辨率
sView.getHolder().setFixedSize(320, 280);
sView.getHolder().setKeepScreenOn(true);
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 0x123 && grantResults.length == 3
&& grantResults[0] == PackageManager.PERMISSION_DENIED
&& grantResults[1] == PackageManager.PERMISSION_DENIED
&& grantResults[2] == PackageManager.PERMISSION_DENIED) {
}
}
}
layout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SurfaceView
android:id="@+id/sView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
</LinearLayout>
</FrameLayout>
权限设置:
<!--获取调用相机的权限、访问存储、录制声音的权限-->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
注意问题1:Android使用MediaRecorder录音导致java.lang.RuntimeException: start failed.如果项目的访问权限设置的没问题,可以注册掉下面的代码。
//设置视频尺寸和每秒12帧
//mRecorder.setVideoSize(640, 560);
//mRecorder.setVideoFrameRate(12);
注意问题2:若安卓6.0以上需要动态获取权限,在MainActivity中加入以下代码
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
return;
}
}
}
————————————————
版权声明:本文为CSDN博主「bao_hugh」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/bao_successor/article/details/104681365
网友评论