Android plays an audio file during a phone call

For my Android application, I want to play an audio file after answering a call from my application. The application will start a phone call, and as soon as the recipient picks up the call, the application should start playing back the recorded audio file.

As a result of searching a lot on Google, I found that this is not possible directly for a non-rotatable device.

But any alternative to this or any other way to achieve this?

I want to implement this because the application’s functionality: if the application user has problems, he can just click the help button in the application, and then the application will start calling the specified contact person and should play the recorded audio file as soon as the person received the call.

+6
source share
1 answer

Read the steps:

1. Some of the built-in music players in the Android device, where they handle it, restrict music when the call is in TelephonyManager.EXTRA_STATE_OFFHOOK (OFFHOOK STATE), so there is no way to play background music using your own players. But in some from a third-party apss, such as "poweramp music palyer", maybe.

2. Using the MediaPlayer class is also not possible (clearly indicated in the documentation)

3. This is only possible in one case, if your developing custom music player (without using the MediaPlayer class) that implements

AudioManager.OnAudioFocusChangeListener, using this, you can get the status of the audio master in the code "focusChange = AUDIOFOCUS_LOSS_TRANSIENT" below (this state causes when the music is playing in the background, any incoming call has come) this state is completely in the hands of the developers, playing or pausing music . As in accordance with your requirements, since for the question you asked if you want to play music when the call is in the OFFHOOK STATE state, do not stop the music playing in the OFFHOOK state. And this is only possible when the headset is turned off.

AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT // Pause playback (during incoming call) } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // Resume playback (incoming call ends) } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { am.unregisterMediaButtonEventReceiver(RemoteControlReceiver); am.abandonAudioFocus(afChangeListener); // Stop playback (when any other app playing music in that situation current app stop the audio) } } }; 
+5
source

All Articles