0

So I made a music app that plays a stream like a radio, but I noticed when my app is playing audio and the user decides to send a voice note on Whatsapp, my app doesn't stop playing the audio. I've implemented the telephone feature where it stops if a call arrives.

Any ideas how I would do that?

I was doing research on Audio Focus but don't think that is what I need here.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
mazen el zoor
  • 305
  • 4
  • 16
  • Check this out. https://stackoverflow.com/questions/11098470/how-to-pause-the-background-music-while-recording-audio-in-android – Dan Sposito Jul 05 '18 at 11:36
  • Thanks @DanSposito for the suggestion, but I've already implemented this and it doesn't do anything sadly – mazen el zoor Jul 05 '18 at 12:12

2 Answers2

0

You need to manage the Audio Focus as explained here: https://developer.android.com/guide/topics/media-apps/audio-focus

Audio Focus is held while someone asks for it and starts to play some audio and in the same time a "system notification is broadcasted" to all who are listening for Audio Focus Changes so they could stop their actions (the recording in your example) to let the audio be the only one. This "lock" could be Exclusive (only the locker can play audio) or "Mixed with the other audio" (both audio will play/record together but the newer one has the louder volume or priority).

emandt
  • 2,547
  • 2
  • 16
  • 20
  • I already have audio focus listener implemented, but that doesn't work for whatsapp voice notes, that's what I'm asking for – mazen el zoor Jul 05 '18 at 13:57
  • 1
    @mazenelzoor I start to think your Audio Focus implementation could be the problem. I just tried and WhatsApp uses Audio Focus to notify when it starts/finish recording a message. The test: (1) play a music using a Media Player like VLC or the stock music player (2) open WhatsApp and start to record a message (3) you will notice that the music stops and starts again when you release WhatsApp button – emandt Jul 06 '18 at 09:05
  • I think you are right but I checked all the code seemed ok. I guess I'll leave it till a later time. Thank you for all the help anyway! – mazen el zoor Jul 11 '18 at 14:13
-1

I haven't seen any callback oriented approaches to solving this, but there was a great solution posted in this SO question:

private boolean validateMicAvailability(){
    Boolean available = true;
    AudioRecord recorder =
            new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                    AudioFormat.CHANNEL_IN_MONO,
                    AudioFormat.ENCODING_DEFAULT, 44100);
    try{
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
            available = false;

        }

        recorder.startRecording();
        if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
            recorder.stop();
            available = false;

        }
        recorder.stop();
    } finally{
        recorder.release();
        recorder = null;
    }

    return available;
}

Only way I could see this working is polling it on a background timed task thread. This post has a couple of answers on how to implement this and im sure you'll find plenty of other examples.

There may be a way to add a callback approach from openSL so that when the state is changed you can duck the music volume or whatever is desired, but it seems like people were struggling to implement this.

WoodyDev
  • 1,386
  • 1
  • 9
  • 19
  • I believe the reason for the downvote was that the `getRecordingState()` returns the state of the object --> which was just created, so it will always return not recording, because `startRecording()` wasn't called (and if it had been, it would always return recording. This returns values for the current instance - not other apps' instances of AudioRecord. – Edw590 Mar 02 '23 at 18:25