javaandroidandroid-mediarecorderandroid-8.0-oreocall-recording

MediaRecorder Start Failed Error


I want to recording a call voice but i get MediaRecorder:start failed : -2147483648

It's my call record code block

   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ı");

    }

What is my error ? Can anyone help me ? I Tried MediaRecorder.AudioSource.VOICE_CALL AND MediaRecorder.AudioSource.VOICE_COMMUNICATION When i used Voice_Communication type i didn't get voice of caller.


Solution

  • code 2147483648 refers to MEDIA_ERROR_SYSTEM (low-level system error).

    Based on the documentation:

    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 might actually not exist anymore since you're within a different BroadcastReceiver instance than the one that created the MediaRecorder. Its not a great idea to perform this task in BroadcastReceiver since it will only execute for 10 seconds after which System could declare app as not responding.

    One solution would be to execute this code to Service