1. Are you ready for the Galaxy S20? Here is everything we know so far!

Record the audio of a phone call (android java development)

Discussion in 'Android Development' started by con7a04, Jan 14, 2021.

  1. con7a04

    con7a04 Lurker
    Thread Starter

    I am developing an app that records call audio. My app is able to record system audio but hearing an audio that the app has recorded I noticed that when I accept the call, the audio seems to stop and only seems to restart when the call is ended. Could somebody tell me why? I am using MediaRecorder. I leave you my code below.

    my code:

    Code (Text):
    1.   @Override
    2.         protected void onCreate(Bundle savedInstanceState) {
    3.             super.onCreate(savedInstanceState);
    4.             setContentView(R.layout.activity_main);
    5.             BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    6.             rec = findViewById(R.id.btn_rec);
    7.             chronometer = findViewById(R.id.chronometer);
    8.             prgwait = findViewById(R.id.wait);
    9.             stop = findViewById(R.id.btn_stop);
    10.    
    11.             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_RECORD_AUDIO_PERMISSION);
    12.    
    13.             rec.setOnClickListener(new View.OnClickListener() {
    14.                 @Override
    15.                 public void onClick(View view) {
    16.    
    17.                     prgwait.setVisibility(View.VISIBLE); //start progress
    18.                     chronometer.setVisibility(View.VISIBLE);
    19.                     rec.setVisibility(View.GONE);
    20.                     prgwait.setVisibility(View.GONE); //stop progress
    21.                     stop.setVisibility(View.VISIBLE);
    22.                     chronometer.setBase(SystemClock.elapsedRealtime());
    23.                     chronometer.start();
    24.                     Toast.makeText(MainActivity.this, "Registration started", Toast.LENGTH_SHORT).show();
    25.                     //start rec
    26.                     recorder = new MediaRecorder();
    27.                     recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    28.                     recorder.setOutputFormat(output_formats[currentFormat]);
    29.                     //recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    30.                     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    31.                     recorder.setOutputFile(getFilename());
    32.                     recorder.setOnErrorListener(errorListener);
    33.                     recorder.setOnInfoListener(infoListener);
    34.    
    35.    
    36.                     try {
    37.                         recorder.prepare();
    38.                         recorder.start();
    39.                     } catch (IllegalStateException e) {
    40.                         Log.e("REDORDING :: ", e.getMessage());
    41.                         e.printStackTrace();
    42.                     } catch (IOException e) {
    43.                         Log.e("REDORDING :: ", e.getMessage());
    44.                         e.printStackTrace();
    45.                     }
    46.    
    47.                     audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE); //record the audio of device
    48.                     audioManager.setMode(AudioManager.MODE_IN_CALL);
    49.                     audioManager.setSpeakerphoneOn(true); //enable the speaker for record the voices of the call
    50.    
    51.                     //end code for rec calls
    52.                 }
    53.             });
    54.    
    55.    
    56.             //stop a rec
    57.    
    58.             stop.setOnClickListener(new View.OnClickListener() {
    59.                 @Override
    60.                 public void onClick(View view) {
    61.    
    62.                     prgwait.setVisibility(View.VISIBLE);   //start progressbar
    63.                     stop.setVisibility(View.GONE);
    64.    
    65.    
    66.    
    67.                         chronometer.stop();
    68.                         chronometer.setVisibility(View.GONE);
    69.                         rec.setVisibility(View.VISIBLE);
    70.    
    71.                         audioManager.setSpeakerphoneOn(false); //turn off the speaker
    72.    
    73.                         try {
    74.                             if (null != recorder) {
    75.                                 recorder.stop();
    76.                                 recorder.reset();
    77.                                 recorder.release();
    78.    
    79.                                 recorder = null;
    80.                             }
    81.                         } catch (RuntimeException stopException) {
    82.    
    83.                         }
    84.                         Toast.makeText(MainActivity.this, "Record saved", Toast.LENGTH_SHORT).show();
    85.                         //end stop code
    86.                         chronometer.setBase(SystemClock.elapsedRealtime());
    87.                         prgwait.setVisibility(View.GONE);
    88.                     }
    89.    
    90.             });
    91.  
    92. private String getFilename() {
    93.         String filepath = Environment.getExternalStorageDirectory().getPath();
    94.         File file = new File(filepath, AUDIO_RECORDER_FOLDER);
    95.  
    96.         if (!file.exists()) {
    97.             file.mkdirs();
    98.         }
    99.  
    100.         return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
    101.     }
    102.  
    103.     private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    104.         @Override
    105.         public void onError(MediaRecorder mr, int what, int extra) {
    106.             Toast.makeText(MainActivity.this,
    107.                     "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    108.         }
    109.     };
    110.  
    111.     private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    112.         @Override
    113.         public void onInfo(MediaRecorder mr, int what, int extra) {
    114.             Toast.makeText(MainActivity.this,
    115.                     "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT)
    116.                     .show();
    117.         }
    118.     };
    119. }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...