Play a music / advertisement when the call is onHold in Android - android

When you place callers on hold. The application should play music / some custom message to the receiver in the background, till the call is in hold. Once the call becomes active, that music / custom message should be stopped. Is there any API's available to do so ?
If so,Please let me know few steps to take it forward.
Thanks in advance.

Android does not allow you to modify the outgoing audio stream during a call. Even though the call is on hold, it is still considered an ongoing call. The best you can do is to play whatever you want to over the loudspeaker and hope the microphone picks it up.

Related

Android notifications not playing at correct volume after VoIP call

I've implemented VoIP calling in a native Android application, and we've discovered a bug that I'm stuck on. I'm using Twilio for our VoIP calls if that helps at all.
If I place an outgoing VoIP call from our app, after the call ends if I put the app in the background and receive a notification, the volume of the notification is about half what it was before I placed the call. It's also not just notifications from my app, but other apps are affected as well. If I swipe my app away from the app history so it is no longer running in the background, then notifications go back to to playing at their correct volume.
This does not happen when I receive an incoming call, even though I've verified that the same code tears down the call Connection whether it's incoming or outgoing.
I've verified that when the notification is coming in, the device's notification volume is still turned all the way up using AudioManager's getStreamVolume(AudioManager.STREAM_NOTIFICATION) API.
Since all apps appear to be affected, not just my own I'm thinking I can safely assume it's not the code that's playing the notification sound that's the problem. I was thinking maybe something related to the VoIP call wasn't being released properly, and the OS itself is playing the tones at a lower volume because it still thinks we're in a call, but I can't find any evidence of that.
I've confirmed that my Connection object for the call is calling onDisconnect(), and destroy().
My ConnectionService is also being destroyed.
The call state at the time of the notification is not CALL_STATE_OFFHOOK according to the TelephonyManager.
Is there anything else you can think of that would cause notifications to play at a reduced volume?
We also experienced this same issue in our VOIP application, although in our case we're using webrtc directly and not Twilio Video -- so I don't know if this exactly applies to your case but maybe can help you find some clues.
In our case, we discovered we were not calling close() on all of the WebRTCPeer objects. That meant after the call ended, an AudioTrack was still active which affected the audio routing... resulting in the very quiet ring/notification sounds.
I would guess the equivalent with the Twilio Video SDK is to make sure that you unpublish and release all audio tracks (and video and data tracks) and then disconnect the Room object.
https://twilio.github.io/twilio-video-android/docs/latest/com/twilio/video/LocalParticipant.html#unpublishTrack-
https://twilio.github.io/twilio-video-android/docs/latest/com/twilio/video/LocalAudioTrack.html#release--
https://twilio.github.io/twilio-video-android/docs/latest/com/twilio/video/Room.html#disconnect--
We found some good clues examining the output of adb shell dumpsys audio -- in the bad state, we could see in the "Stream Volumes" section that the device for the ring/alarm/notification streams was stuck on "earpiece" rather than "speaker", and that there was an extra AudioTrack in the "Players" section.
Maybe this gives you some ideas to try... good luck!

Release AudioRecord android when other app request for recording

I have an audio recording service in my app which will record the sound continuously. So, it will always occupy the AudioRecord. It means no other app can use audio recorder as it is already occupied by the service. Is there any way to notify that other app is requesting for audio recorder(so that I can release it) and also when the app releases it(so that I can assign it back to the service)?
Maybe a possible way is to create a BroadcastReceiver which receives an event from the app which is requesting the control over the mic source. The onReceive() method should interact with the service and release the resource. When the other app is finishing it can revert the process to start the service again. If you can't get control over the behavior of the requesting app I think there's a slightly different problem. Anyway:
The problem is all about knowing when the resource is being requested, this can be done through AudioManager intent types.
Be sure to check Managing audio focus which talks about audio focus loss in TRANSIENT way!
As #Rekire mentioned, there is possibly no way to achieve this. Also, AudioManager provide no such broadcasts, so it is not possible for different apps. Maybe rooting the device is the only option.
This can be done with AudioManager.OnAudioFocusChangeListener callback. Just stop recording on AUDIOFOCUS_LOSS_TRANSIENT event and start again on AUDIOFOCUS_GAIN event.
This solution works well for Google Voice Search (Google Search widget, Google Chrome, etc).
But unfortunately it works poorly for other ordinary applications (for example HTC M7 Voice Recorder app is not able to start recording on first click on "Record" button, second click do the trick - it seems app should be ready to retry recording on failure several times).

Pause/Stop/Mute music at service interupts

I basically have an audio application that will be playing some music. I want to be able to pause/stop/mute the music when there is an interrupt.
These interrupts include: GPS directions, Phone Call, GPS, etc. (if there are more audio interupts, please let me know)
I already implemented the phone call interrupt, stops the music when phone call received and plays after phone call ends.
How would I do the other interrupts?
EDIT:
I noticed that Android's Play Music application does this. But I am unable to find the source code of that, not sure if that would be helpful.
Make sure you correctly ask for and release Audio Focus as described here:
http://developer.android.com/training/managing-audio/audio-focus.html
With multiple apps potentially playing audio it's important to think about how they should interact. To avoid every music app playing at the same time, Android uses audio focus to moderate audio playback—only apps that hold the audio focus should play audio.
Basically this allows the framework to handle interrupts properly as you cannot specifically code for every situation.

Continue to play audio even if the screen is locked

I am just messing around with an app that streams audio and I wanted to give it a feature similar to Pandora/Google Music/etc where if you press home or lock the screen the audio continues to play in the background.
How exactly can I accomplish this? Is it through a broadcast receiver or a service? If I knew more closely what I was looking for Google would be more helpful.
Thanks!
It's a service. Anytime you want to do something that takes a lot of time like playing music, downloading a lot of data from a server, etc. it should be a service. The basic technique is to always have the service play the music and then have your activity connect to it to show the status and update the tracks, etc.

Stoping other services like receving call and media player when my application opens?

When i open my application , i should not receive any phone call or sms i should stop the media player also. can any one tel me is there any API is available to disable and enable them again once after my application ends.please help me..
Thanks
RAM
It is impossible to block the phone call as referring to this article.
For intercepting coming sms, you may be interested in that thread. Apparently there is a way to do that, but not a clean one.

Categories

Resources