音创按钮
时间: 2023-09-01 18:10:43 浏览: 50
如果你想在 Android 应用中添加一个音创按钮,可以使用 Android 自带的 `MediaRecorder` 类和 `AudioRecord` 类。
下面是一个简单的示例代码,可以录制音频并保存到本地:
```java
private MediaRecorder mRecorder;
private boolean mIsRecording = false;
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(getExternalCacheDir().getAbsolutePath() + "/record.3gp");
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
mRecorder.start();
mIsRecording = true;
} catch (IOException e) {
e.printStackTrace();
}
}
private void stopRecording() {
if (mIsRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mIsRecording = false;
}
}
```
在这个示例中,我们使用 `MediaRecorder` 类来录制音频。我们设置音频输入源为麦克风 (`MediaRecorder.AudioSource.MIC`),输出格式为 3GP (`MediaRecorder.OutputFormat.THREE_GPP`),输出文件名为 `record.3gp`,音频编码器为 AMR_NB (`MediaRecorder.AudioEncoder.AMR_NB`)。然后调用 `prepare()` 方法准备录制,调用 `start()` 方法开始录制。录制完成后,调用 `stop()` 方法停止录制,并释放资源。
当然,如果你想更加精细地控制录制过程,可以使用 `AudioRecord` 类来实现。使用 `AudioRecord` 类需要进行更多的设置和操作,但可以实现更高质量的音频录制。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)