如何在Android中录制电话
我需要在我的 Android 应用程序中录制电话。我尝试过使用 MediaRecorder 将 AudioSource 设置为 MIC、VOICE_COMMUNICATION、VOICE_CALL 和其他选项。但他们都没有记录通话。任何人都可以建议在 android 中录制电话的任何解决方案吗?
以下是我尝试过的代码。但它不会记录通话。只记录通话前后的语音。请提出任何解决方案。
开始录制的代码
public void startRecording(){
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO);
} else {
setReorder();
}
}
public void setReorder(){
audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
recorder = new MediaRecorder();
// recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the file write
prepareAndStart();
} else {
// Request permission from the user
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
}
}
public void prepareAndStart() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
} else {
startRec();
}
}
public void startRec(){
try{
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
}
}
// @SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 0:
// Re-attempt file write
setReorder();
case 1:
prepareAndStart();
case 2:
startRec();
}
}
获取输出文件的文件名的
private String getFilename() {
// String filepath = Environment.getExternalStorageDirectory().getPath();
String filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
Log.d("FILEPATH", filepath);
if (!file.exists()) {
file.mkdirs();
Log.d( "!file.exists","created file");
}
Log.d("BEFORE RETURN", "created file EXISTS");
Log.d("BEFORE RETURN", file.getAbsolutePath());
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
// return (file.getAbsolutePath());
}
代码 停止录制的代码
public void stopRecording(){
audioManager.setSpeakerphoneOn(false);
try{
if (null != recorder) {
recorder.stop();
Log.d("REDORDING STOP :: ", "recorder.stop();");
recorder.reset();
Log.d("REDORDING STOP :: ", "recorder.reset();");
recorder.release();
Log.d("REDORDING STOP :: ", "recorder.release();");
recorder = null;
}
}catch(RuntimeException stopException){
Log.e("REDORDING STOP :: ", "RuntimeException stopException");
Log.e("REDORDING STOP :: ",stopException.getMessage());
stopException.printStackTrace();
}
}
I need to record a phone call in my android application. I have tried with MediaRecorder with the AudioSource set to MIC, VOICE_COMMUNICATION, VOICE_CALL and other options. But none of them record the call. Can anyone please suggest any solution for record a phone call in android.
following is the code that I have tried. But it does not record the call. only record the voice before and after the call. Please suggest Any Solution.
Code to start recording
public void startRecording(){
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO);
} else {
setReorder();
}
}
public void setReorder(){
audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
recorder = new MediaRecorder();
// recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the file write
prepareAndStart();
} else {
// Request permission from the user
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE);
}
}
public void prepareAndStart() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
} else {
startRec();
}
}
public void startRec(){
try{
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("REDORDING :: ",e.getMessage());
e.printStackTrace();
}
}
// @SuppressLint("MissingSuperCall")
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 0:
// Re-attempt file write
setReorder();
case 1:
prepareAndStart();
case 2:
startRec();
}
}
Code to get file name for output file
private String getFilename() {
// String filepath = Environment.getExternalStorageDirectory().getPath();
String filepath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath();
File file = new File(filepath, AUDIO_RECORDER_FOLDER);
Log.d("FILEPATH", filepath);
if (!file.exists()) {
file.mkdirs();
Log.d( "!file.exists","created file");
}
Log.d("BEFORE RETURN", "created file EXISTS");
Log.d("BEFORE RETURN", file.getAbsolutePath());
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
// return (file.getAbsolutePath());
}
Code to stop recording
public void stopRecording(){
audioManager.setSpeakerphoneOn(false);
try{
if (null != recorder) {
recorder.stop();
Log.d("REDORDING STOP :: ", "recorder.stop();");
recorder.reset();
Log.d("REDORDING STOP :: ", "recorder.reset();");
recorder.release();
Log.d("REDORDING STOP :: ", "recorder.release();");
recorder = null;
}
}catch(RuntimeException stopException){
Log.e("REDORDING STOP :: ", "RuntimeException stopException");
Log.e("REDORDING STOP :: ",stopException.getMessage());
stopException.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当电话呼叫开始时,麦克风访问将转移到优先应用程序,即语音呼叫本身及其处理应用程序。如果您的应用程序是普通应用程序,则将无法再访问麦克风。但是,如果您的应用有辅助功能服务,您可以共享麦克风
https:// /developer.android.com/guide/topics/media/sharing-audio-input
据我所知,
VOICE_COMMUNICATION
也只是一个麦克风,上面有一些过滤器,所以在这种情况下相同的麦克风共享应该可以工作最后
VOICE_CALL
现在事情就这样了这里很有趣。此源需要 Manifest.permission.CAPTURE_AUDIO_OUTPUT 权限。如果你的应用程序没有它就会崩溃。问题是此权限不会以任何方式提供给第三方应用程序。可访问性技巧在这里对你没有帮助。现在,如果您只想要录音可访问性+麦克风在大多数情况下都可以工作,并且根据具体情况,您的录音将变得很好或难以听。如果您不想获取 root 权限的手机或自定义 ROM 等,这是您录制通话的唯一方法。
如果您可以对设备进行 root 或自定义 ROM,那么您可以考虑将应用程序安装为系统应用程序,以获得所需的权限。
When the phone call starts Mic access is moved to the priority app which is the voice call itself and its handling app. Your app will no longer have access to the mic if it's an ordinary app. However if your app has an Accessibility service you can share the microphone
https://developer.android.com/guide/topics/media/sharing-audio-input
As far as I know,
VOICE_COMMUNICATION
is also just a mic with some filter on top, so the same mic sharing should work in this caseLastly
VOICE_CALL
Now things get interesting here. This source requires Manifest.permission.CAPTURE_AUDIO_OUTPUT permission. If your app doesn't have it will crash. The problem is this permission is not provided to third-party app in any way. The accessibility trick will not help you here.Now if you just want a recording accessiblity+mic works most of the time and depending on the situation your recording will turn out either fine or unlistenable. If you don't want rooted phone or custom ROM etc. this is the only way you can record a call.
If you can root or custom ROM your device then you can look into installing your app as system application in order to get the permissions you need.
不幸的是,在大多数未root的Android手机上无法使用常规应用程序记录电话。
您需要:
A)使用 OEM 证书签署您的应用程序以获得升级的权限
B)对手机进行 Root
C)查找安全漏洞
我认为有一些罕见的型号具有不同的音频路径配置,可以允许这样做,但数量很少。
Unfortunately, it is not possible on most unrooted Android phones to record phone calls using a regular app.
You would need to either:
A) Sign your app with the OEM certificate to get escalated privileges
B) Root the phone
C) Find a security exploit
I think there are some rare models that have a different configuration of audio pathing that can allow this, but there are very few of them.