📅  最后修改于: 2023-12-03 14:39:11.680000             🧑  作者: Mango
在Android平台上,我们可以使用录音机来录制音频。录音机是一种非常常见的功能,通常用于开发语音记录、音频拨号、语音识别等应用程序。本文将介绍如何在Android应用程序中使用录音机。
在使用录音机之前,我们需要在AndroidManifest.xml文件中添加相应的权限。这样才能够获取到录音机的硬件资源并进行录音操作。
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在Android 6.0及以上版本中,需要动态申请权限。我们在应用程序启动时需要检查是否已经获得了录音所需的权限。
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private boolean permissionToRecordAccepted = false;
private String[] permissions = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private void checkPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
} else {
permissionToRecordAccepted = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_RECORD_AUDIO_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionToRecordAccepted = true;
}
break;
}
if (!permissionToRecordAccepted) {
// 权限未被授予,处理相应逻辑
}
}
使用录音机进行录音的步骤如下:
private MediaRecorder mediaRecorder;
private String recordFilePath;
private void prepareRecording() {
// 创建录音机实例
mediaRecorder = new MediaRecorder();
// 设置音源为麦克风
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置输出格式为3gp
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置音频编码器为AMR_NB
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置录音保存路径
recordFilePath = getExternalCacheDir().getAbsolutePath() + "/record.3gp";
mediaRecorder.setOutputFile(recordFilePath);
try {
// 准备录音
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private void startRecording() {
if (mediaRecorder != null) {
// 开始录音
mediaRecorder.start();
}
}
private void stopRecording() {
if (mediaRecorder != null) {
// 停止录音
mediaRecorder.stop();
// 释放录音机资源
mediaRecorder.release();
mediaRecorder = null;
}
}
下面是一个简单的示例,展示了如何使用录音机进行录音:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_RECORD_AUDIO_PERMISSION = 200;
private boolean permissionToRecordAccepted = false;
private String[] permissions = {Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};
private MediaRecorder mediaRecorder;
private String recordFilePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermissions();
Button startButton = findViewById(R.id.start_button);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prepareRecording();
startRecording();
}
});
Button stopButton = findViewById(R.id.stop_button);
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopRecording();
}
});
}
private void checkPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions, REQUEST_RECORD_AUDIO_PERMISSION);
} else {
permissionToRecordAccepted = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_RECORD_AUDIO_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionToRecordAccepted = true;
}
break;
}
if (!permissionToRecordAccepted) {
// 权限未被授予,处理相应逻辑
}
}
private void prepareRecording() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recordFilePath = getExternalCacheDir().getAbsolutePath() + "/record.3gp";
mediaRecorder.setOutputFile(recordFilePath);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private void startRecording() {
if (mediaRecorder != null) {
mediaRecorder.start();
Toast.makeText(this, "开始录音", Toast.LENGTH_SHORT).show();
}
}
private void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
Toast.makeText(this, "录音结束", Toast.LENGTH_SHORT).show();
}
}
}
这是一个简单的Android录音机示例,展示了如何使用录音机进行录音操作。通过检查和申请相应权限,准备录音,开始录音和停止录音,我们可以很容易地在Android应用程序中实现录音功能。
以上就是Android中录音机的介绍及使用示例。希望对你有所帮助!