MediaRecorder启动失败错误

时间:2018-05-09 15:28:41

标签: java android android-mediarecorder android-8.0-oreo call-recording

我想录制一个呼叫语音,但我得到MediaRecorder:开始失败:-2147483648

这是我的通话记录代码块

   public void SesKayitBaslat(String number) {

        Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();

        String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date());
        File sampleDir = new File(Environment.getExternalStorageDirectory(), "/ASesKaydi");
        if (!sampleDir.exists()) {
            sampleDir.mkdirs();
        }
        String file_name = "Record";
        try {
            audiofile = File.createTempFile(file_name, ".amr", sampleDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();

        recorder  = new MediaRecorder();
                          //recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);

        recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            Log.e("Eror","1");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Eror","2");
            e.printStackTrace();
        }
        if(!recordstarted)
        {

            recorder.start();
            recordstarted = true;
        }
        Log.e("Kayit:", "Başladı");

    }

我的错误是什么?谁能帮我 ?我试过MediaRecorder.AudioSource.VOICE_CALL和MediaRecorder.AudioSource.VOICE_COMMUNICATION 当我使用Voice_Communication类型时,我没有得到来电者的声音。

1 个答案:

答案 0 :(得分:1)

代码2147483648是指MEDIA_ERROR_SYSTEM(低级系统错误)。

基于文档:

  

BroadcastReceiver对象仅在通话期间有效   to onReceive(Context,Intent)。一旦你的代码从此返回   功能,系统认为要完成的对象不再   活性

换句话说,您期望在那里的MediaRecorder实例可能实际上不再存在,因为您在与创建MediaRecorder的实例不同的BroadcastReceiver实例中。在BroadcastReceiver中执行此任务不是一个好主意,因为它只会执行10秒钟,之后系统可能会将应用程序声明为无响应。

一种解决方案是将此代码执行到Service