Android 2.2 中的通话录音
我编写了这段代码来记录通话。它在 Android 2.1 中运行良好。在Android 2.2中,它创建一个0字节的输出文件。
我该如何解决这个问题?
MediaRecorder _recorder = new MediaRecorder();
public void start() throws IOException {
try {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/sam.wav").getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
_recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL );
_recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test.wav");
_recorder.prepare();
_recorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
I have written this code for recording Calls. It works fine in Android 2.1. In Android 2.2, it creates an output file with 0 bytes.
How I can solve this?
MediaRecorder _recorder = new MediaRecorder();
public void start() throws IOException {
try {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/sam.wav").getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
_recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL );
_recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test.wav");
_recorder.prepare();
_recorder.start();
} catch (Exception e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此代码片段
代替
Use this snippet
instead of
通话录音仅适用于部分 Android 手机。它可能适用于一部运行 2.1 的手机,但不适用于运行 2.2 的其他型号。尽管该 API 将在所有架构上编译和运行,但某些设备在硬件中禁用了此功能。
请参阅如何在 Android 中录制语音和通话录音? 了解更多详情。
Call recording only works on some Android phones. It might work on one phone running 2.1, but not on a different model running 2.2. Although the API will compile and run on all architectures, some devices have disabled this feature in the hardware.
See How can I record voice and record Call in Android? for more details.