如何使用AudioSource.VOICE_CALL

时间:2017-07-14 08:19:24

标签: android recorder

尝试录制通话时,我使用MediaRecorder class时使用AudioSource.MICAudioSource.VOICE_COMMUNICATION仅录制我的声音,而不是来自收听者 当我使用AudioSource.VOICE_CALL时,它会在接听电话时给出例外情况。 这是代码

if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
            if((bundle = intent.getExtras()) != null){
                state = bundle.getString(TelephonyManager.EXTRA_STATE);
                if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                    inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    wasRinging = true;
                    Toast.makeText(context, inCall + " is calling", Toast.LENGTH_SHORT).show();
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    if(wasRinging){
                        Toast.makeText(context, "Call Answered", Toast.LENGTH_SHORT).show();
                        Date date = new Date();
                        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");

                        String filename = "rec_" + format.format(date) + ".mp3";
                        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath();
                        String fileUri = path + "/" + filename;
                        Log.v("testing uri", fileUri);
                        File file = new File(fileUri);

                        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
                        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                        recorder.setOutputFile(file.getAbsolutePath());

                        try {
                            recorder.prepare();
                            recorder.start();
                            recording = true;

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                    wasRinging = false;
                    Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
                    if(recording && recorder != null){
                        recorder.stop();
                        recorder = null;
                        recording = false;
                    }
                }
            }
        }

这是错误..

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } in com.asadullah.callrecorder.MyBroadCastReceiver@41d6c7a8
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5102)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: start failed.
   at android.media.MediaRecorder.start(Native Method)
   at com.asadullah.callrecorder.MyBroadCastReceiver.onReceive(MyBroadCastReceiver.java:62)
   at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)

表明权限是:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.STORAGE" />

5 个答案:

答案 0 :(得分:2)

VOICE_CALL现已弃用。这就是发生此错误的原因。

使用VOICE_COMMUNICATION作为AudioSource,因为它是针对语音通信(如VoIP)调整的麦克风音频源。

我也在使用通话录音应用,但在Android 7.1.1中失败了

如果您没有在Android 7.1.1上尝试通话记录,则代码将起作用。

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

答案 1 :(得分:1)

要使用VOICE_CALL,您需要获得特别许可。

 <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"
   tools:ignore="ProtectedPermissions" />

答案 2 :(得分:0)

我认为在较新版本的Android(Api 23)中录制之前需要一些权限。 看看这个问题 - Recording calls in android why this not works

<强>权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

如果您需要更多帮助,您甚至可以查看官方document以获取有关此错误的更多信息。

答案 3 :(得分:0)

您需要Voice_Call的CAPTURE_AUDIO_OUTPUT权限,is documented here

但是你不会因为stated here而获得许可。

请注意第&#34; 不适用于第三方应用。&#34;

答案 4 :(得分:0)

我想你忘记了这个许可。

    <uses-permission android:name="android.permission.CALL_PHONE" />