java.lang.RuntimeException:在android.media.MediaRecorder.start(Native Method)启动失败

时间:2016-07-20 10:42:26

标签: android android-studio mediarecorder android-mediarecorder

我正在打电话记录。当我开始记录电话时,不幸的是停止了。 &安培;它给出错误MediaRecorder启动失败-2147483648。我请告诉我代码中的问题是什么?这是我的代码。

public class incomingcall extends BroadcastReceiver {
Context c;
MediaRecorder recorder;
public incomingcall() {
}
@Override
public void onReceive(Context context, Intent intent) {
    c = context;
    try {
        PhoneStateChangeListener pscl = new PhoneStateChangeListener();
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);
    } catch (Exception e) {
        Log.e("", "", e);
    }
}

private class PhoneStateChangeListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(c, "ring", Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                startRecording();
                Toast.makeText(c, "offhook", Toast.LENGTH_SHORT).show();
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                stopRecording();
                Toast.makeText(c, "idle", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
private void startRecording() {
    try {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        String file=c.getFilesDir().getAbsolutePath();
        file+="/sound.3gp";
        recorder.setOutputFile(file);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.prepare();
        recorder.start();
    } catch (IOException e) {
        Log.e("", "prepare() failed", e);
    }
}
private void stopRecording() {
    try {
        recorder.stop();
        recorder.release();
        recorder = null;
    } catch (Exception e) {
        Log.e("", "", e);
    }
 }
}

日志

    07-20 15:33:47.867 18525-18525/in.pounkumar.callblocker E/MediaRecorder: start failed: -2147483648
07-20 15:33:47.868 18525-18525/in.pounkumar.callblocker E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: in.pounkumar.callblocker, PID: 18525
                                                                          java.lang.RuntimeException: start failed.
                                                                              at android.media.MediaRecorder.start(Native Method)
                                                                              at in.pounkumar.callblocker.incomingcall.startRecording(incomingcall.java:73)
                                                                              at in.pounkumar.callblocker.incomingcall.access$100(incomingcall.java:20)
                                                                              at in.pounkumar.callblocker.incomingcall$PhoneStateChangeListener.onCallStateChanged(incomingcall.java:53)
                                                                              at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:295)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我通过对三星和LG设备使用不同的初始化来解决它。

        String manufacturer = Build.MANUFACTURER;
    if (manufacturer.toLowerCase().contains("samsung")) {
        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
    } else {
        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
    }

希望它对你有所帮助。

答案 1 :(得分:0)

MediaRecorder.AudioSource.VOICE_CALL源需要CAPTURE_AUDIO_OUTPUT权限。 See Android Docs

您是否已在清单中添加此权限

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

答案 2 :(得分:-1)

MediaRecorder.start方法在您的情况下失败,原因如下:

recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

MediaRecorder.AudioSource.VOICE_CALL来源不起作用的原因取决于您的设备制造商/国家/地区等。

因此,这会引发异常,因为您的设备支持MediaRecorder.AudioSource.VOICE_CALL

尝试将其更改为MediaRecorder.AudioSource.MIC,它会起作用:

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

阅读this了解详情。