如何在 Android 中录制语音?
我正在尝试在 android 中录制语音,但它会在路径(sdcard/文件名)上创建 .mp3 文件,但是当我运行此文件时,它不会播放,因为它不录制语音。
这是我的代码
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case(R.id.Button01):
try {
//audio.start();
startRecord();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case(R.id.Button02):
//audio.stop();
stopRecord();
}
}
private void startRecord() throws IllegalStateException, IOException{
// recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator?
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/Music/"+System.currentTimeMillis()+".amr");
recorder.prepare();
recorder.start();
}
private void stopRecord(){
recorder.stop();
//recorder.release();
}
}
清单文件
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am trying to record the voice in android But it will create the .mp3 file on the path (sdcard/filename) But when i run this file it doesen't play because it doesn't record the voice.
Here is My code
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case(R.id.Button01):
try {
//audio.start();
startRecord();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
case(R.id.Button02):
//audio.stop();
stopRecord();
}
}
private void startRecord() throws IllegalStateException, IOException{
// recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator?
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/Music/"+System.currentTimeMillis()+".amr");
recorder.prepare();
recorder.start();
}
private void stopRecord(){
recorder.stop();
//recorder.release();
}
}
Manifest file
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有关录制音频和回放的信息,请参阅 Android 音频捕获文档录制的音频。
Refer to the Android Audio Capture documentation for recording audio and playing back the recorded audio.
如果您搜索了 google,您会在他们的 API 指南中找到此内容:
SOURCE< /a>
if you searched google, you'll find this in their API Guides:
SOURCE
来自 Google 的官方示例链接
https://developer.android.com/guide/topics/media/ mediarecorder.html#example
我建议仅遵循该方法,因为它保证是最新且安全的。
Offical Example link from Google
https://developer.android.com/guide/topics/media/mediarecorder.html#example
I suggest to only follow that one since its guaranteed to be up-to-date and secure.
获取清单权限
MainActivity 代码
AudioRecorder 类代码如下:
}
*** 在活动启动时获取用户权限。 MANAGE_EXTERNAL_STORAGE 权限和 RECORD_AUDIO 权限。
Get manifests permission
MainActivity code
AudioRecorder class code here:
}
*** Get the user permission when the activity will start. MANAGE_EXTERNAL_STORAGE permission and RECORD_AUDIO permission.