问题
I am working on records a phone calls. When i start a record a phone call then it's unfortunately stop. & it's gives error MediaRecorder start fail -2147483648. I Please tell me what is the problem in my code? Here is my Code.
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);
}
}
}
Log
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)
回答1:
I have the same problem and i solved it by using different initializations for Samsung and LG devices.
String manufacturer = Build.MANUFACTURER;
if (manufacturer.toLowerCase().contains("samsung")) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
Hope it helps you.
回答2:
MediaRecorder.AudioSource.VOICE_CALL source requires the CAPTURE_AUDIO_OUTPUT permission. See Android Docs
Have you added this permission in your manifest
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
回答3:
The MediaRecorder.start
method fails in your case, due to this :
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
The reason why MediaRecorder.AudioSource.VOICE_CALL
source doesn't work depends on your device manufacturer/country etc.
So, this is throwing an exception as MediaRecorder.AudioSource.VOICE_CALL
is NOT supported on your device.
Try changing it to MediaRecorder.AudioSource.MIC
and it will work :
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Read this for more details.
来源:https://stackoverflow.com/questions/38479041/java-lang-runtimeexception-start-failed-at-android-media-mediarecorder-startna