开发者社区> 问答> 正文

MediaRecorder start failed: -2147483648?报错

在 recorder.start();在4.2模拟器上启动录音时报错,不知道为何...在4.3上stop()报异常。

初次接触Android,请大神们指教


public class PhoneRecordService extends Service {

    public void onCreate() {
        super.onCreate();
        Log.i(tag, "电话录音服务创建...");
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        tm.listen(new MyPhoneStatusListener(),
        PhoneStateListener.LISTEN_CALL_STATE);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



private class MyPhoneStatusListener extends PhoneStateListener {


    private MediaRecorder recorder = null;
    private String phoneNumber = null;
    boolean isRecording = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:// 闲置
                if (null != recorder && isRecording) { //正在录音
                    recorder.stop();
                    recorder.reset(); // You can reuse the object by going back to setAudioSource() step
                    recorder.release(); // Now the object cannot be reused
                    recorder = null;
                    isRecording = false;
                    Log.i(tag, "结束录音..." + phoneNumber);
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:// 响铃
                phoneNumber = incomingNumber;
                prepareRecorder();
                Log.i(tag, "准备录音..." + phoneNumber);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:// 接通
                prepareRecorder();
                if(!isRecording){
                    recorder.start(); // Recording is now started
                    isRecording = true;
                    Log.i(tag, "开始录音..." + phoneNumber);
                }
            break;
        }
    }


    private void prepareRecorder() {
        if(null == recorder){
            recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            String dirPath = FileUtils.createSDCardDir("/monitor");
            recorder.setOutputFile(dirPath + System.currentTimeMillis() + "_" + phoneNumber + ".amr");
            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}


异常:

08-11 09:24:01.608: E/MediaRecorder(2412): start failed: -2147483648

08-11 09:24:01.618: D/AndroidRuntime(2412): Shutting down VM
08-11 09:24:01.618: W/dalvikvm(2412): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-11 09:24:01.628: E/AndroidRuntime(2412): FATAL EXCEPTION: main
08-11 09:24:01.628: E/AndroidRuntime(2412): java.lang.RuntimeException: start failed.
08-11 09:24:01.628: E/AndroidRuntime(2412): at android.media.MediaRecorder.start(Native Method)
08-11 09:24:01.628: E/AndroidRuntime(2412): at com.liwp.monitor.service.PhoneRecordService$MyPhoneStatusListener.onCallStateChanged(PhoneRecordService.java:79)
08-11 09:24:01.628: E/AndroidRuntime(2412): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:369)
08-11 09:24:01.628: E/AndroidRuntime(2412): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 09:24:01.628: E/AndroidRuntime(2412): at android.os.Looper.loop(Looper.java:137)
08-11 09:24:01.628: E/AndroidRuntime(2412): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-11 09:24:01.628: E/AndroidRuntime(2412): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 09:24:01.628: E/AndroidRuntime(2412): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 09:24:01.628: E/AndroidRuntime(2412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-11 09:24:01.628: E/AndroidRuntime(2412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-11 09:24:01.628: E/AndroidRuntime(2412): at dalvik.system.NativeStart.main(Native Method)
08-11 09:29:01.758: I/Process(2412): Sending signal. PID: 2412 SIG: 9

展开
收起
爱吃鱼的程序员 2020-06-14 22:52:41 1203 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB


    对于start()方法的说明:

    void android. media. MediaRecorder.start()throws IllegalStateException

    请教最后是怎么处理的呢?????、

    是不是没有开启麦克风权限啊

    2020-06-14 22:52:58
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SPEED MATTERS:HOW TO PROCESS B 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载