问题
I intend to record calls with this application. But when I set the audioSource to MediaRecorder.AudioSource.VOICE_CALL, it gives an error but when the audioSource is set to MediaRecorder.AudioSource.MIC, it works perfectly fine. I am not sure where is the problem. The logcat of the problem is below. Any form of help is greatly appreciated. Thanks.
public class IncomingCallReceiver extends BroadcastReceiver {
private MediaRecorder mRecorder;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Log.i("TelephonyManager", "Call picked up");
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setAudioEncodingBitRate(16);
mRecorder.setAudioSamplingRate(44100);
mRecorder.setOutputFile("/sdcard/Recording/callrecord.mp4");
try{
mRecorder.prepare();
}
catch(IOException e){
}
mRecorder.start();
Log.i("StartRecordingCall", "Recording Call end");
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
Log.i("TelephonyManager", "Call hunged up");
mRecorder.stop();
mRecorder.release();
mRecorder=null;
}
}
/
12-18 20:15:56.755: E/MediaRecorder(1577): start failed: -2147483648
12-18 20:15:56.755: D/AndroidRuntime(1577): Shutting down VM
12-18 20:15:56.755: W/dalvikvm(1577): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
12-18 20:15:56.765: E/AndroidRuntime(1577): FATAL EXCEPTION: main
12-18 20:15:56.765: E/AndroidRuntime(1577): java.lang.RuntimeException: start failed.
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.media.MediaRecorder.start(Native Method)
12-18 20:15:56.765: E/AndroidRuntime(1577): at com.example.callrecorder.MainActivity.startRecording(MainActivity.java:447)
12-18 20:15:56.765: E/AndroidRuntime(1577): at com.example.callrecorder.MainActivity.onClick(MainActivity.java:279)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.view.View.performClick(View.java:3534)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.view.View$PerformClick.run(View.java:14263)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.os.Handler.handleCallback(Handler.java:605)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.os.Looper.loop(Looper.java:137)
12-18 20:15:56.765: E/AndroidRuntime(1577): at android.app.ActivityThread.main(ActivityThread.java:4441)
12-18 20:15:56.765: E/AndroidRuntime(1577): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 20:15:56.765: E/AndroidRuntime(1577): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 20:15:56.765: E/AndroidRuntime(1577): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-18 20:15:56.765: E/AndroidRuntime(1577): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-18 20:15:56.765: E/AndroidRuntime(1577): at dalvik.system.NativeStart.main(Native Method)
回答1:
It's possible that the device you're running your app on either doesn't support voice call recording at all, or that it doesn't like one or more of the parameters you've tried to set.
For example, you could try using an 8000 Hz sample rate instead of 44100 Hz (44100 Hz makes no sense for AMR-NB anyway) and getting rid of the call to setAudioEncodingBitRate
altogether.
Another potential problem with your code is that you've got the MediaRecorder
instance in your BroadcastReceiver
object, and haven't declared it static. Here's what the Android documentation has to say about BroadcastReceivers:
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.
In other words, the MediaRecorder
instance that you expect to be there when you get a broadcast containing EXTRA_STATE_IDLE
might actually not exist anymore since you're within a different BroadcastReceiver
instance than the one that created the MediaRecorder
.
来源:https://stackoverflow.com/questions/13933065/mediarecorder-start-fail-2147483648