android 语音录制播放
时间: 2023-07-22 22:37:17 浏览: 64
要在 Android 上实现语音录制和播放,你可以使用 Android 提供的 MediaRecorder 和 MediaPlayer 类。
以下是一个简单的示例,演示如何录制音频并将其保存到文件中:
```java
// 开始录音
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile);
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}
// 停止录音
recorder.stop();
recorder.release();
recorder = null;
```
接下来,演示如何播放先前录制的音频文件:
```java
// 播放录音文件
MediaPlayer player = new MediaPlayer();
try {
player.setDataSource(outputFile);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
// 停止播放
player.release();
player = null;
```
请注意,以上示例中的 `outputFile` 是你要保存录制音频的文件路径。你需要在 Android Manifest 文件中声明“RECORD_AUDIO”和“WRITE_EXTERNAL_STORAGE”权限,以便应用程序可以访问麦克风并将文件保存到设备上的外部存储器中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)