我制作了一个自动呼叫记录器。
我使用媒体记录器
但是当我回复时,我收到一个错误:java.lang.RuntimeException:start failed。
应用程序崩溃,在文件夹中创建文件(3-6kb),但它是空的
所有权限均在
上代码:
public class CallReceiver extends BroadcastReceiver {
private MediaRecorder rec;
private boolean recordstarted;
private File file;
String path="/sdcard/alarms";
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS);
rec = new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.MIC);
rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
rec.setOutputFile(file.getAbsolutePath()+"/"+"rec.3gp");
rec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
final PhoneStateListener listener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
//mContext = context;
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
rec.stop();
rec.reset();
rec.release();
recordstarted=false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
try {
rec.prepare();
} catch (IOException e) {
e.printStackTrace();
}
rec.start();
recordstarted=true;
case TelephonyManager.CALL_STATE_RINGING:
autoPickCall(context);
break;
}
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
例外:
02-27 10:47:04.101 2312-2312/com.phonedialer.phonedialer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.phonedialer.phonedialer, PID: 2312
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.test.myapplication.CallReceiver$1.onCallStateChanged(CallReceiver.java:63)
at android.telephony.PhoneStateListener$1.handleMessage(PhoneStateListener.java:293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)