Android: music volume stays low after audio focus gained - android

In my player app (on Android 6.0) the music volume (of my own player, not the media player api) remains low/ ducked after the audio focus returns to the app.
I'm not ducking anything myself when the audio focus gets lost, just stopping and resuming the playback on the respective event types AUDIOFOCUS_GAIN / AUDIOFOCUS_LOSS of my OnAudioFocusChangeListener implementation.
I have already tried to explicitly set the stream_music volume to max or to call
adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
but with no success. My app needs to be restarted then the volume level is normal again.
Whereas the Android media player behaves correctly, it raises the volume after a phone call is finished. It is just with my app.
How can I force returning to normal volume programatically ?

Related

How to check if external media player is playing in android?

I want to stop my media player if external media player is playing.
I've implemented following code but it is called even when my app media player is playing.
if (audioManager.isMusicActive()) {
return;
}
How to distinguish between in-app media player and external media player?
Any help would be appreciated.
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
Further,
A well-behaved audio app should manage audio focus according to these
general guidelines:
Call requestAudioFocus() immediately before starting to play and
verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design
your app as we describe in this guide, the call to requestAudioFocus()
should be made in the onPlay() callback of your media session. When
another app gains audio focus, stop or pause playing, or duck the
volume down. When playback stops, abandon audio focus. Audio focus is
handled differently depending on the the version of Android that is
running:
You can check the more detail from developer link.

How can i use the the physical volume button on the phone to change the volume of applications?

I use android.media.MediaPlayer to play some sounds in my app, but when i press the physical volume button, it only changes the system volume (ringtone volume). I searched in google but i haven't had any idea yet.
I use android.media.MediaPlayer not Audiomanger.
Call the following right before you start playing music
setVolumeControlStream(AudioManager.STREAM_MUSIC);
and call the following after you finish
setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
This makes your volume rocker control all music streams and then resets it to default behavior (depends on what's happening).
Don't forget to call myMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC) before myMediaPlayer.prepare(). Replace STREAM_MUSIC in both calls with some other value if it fits the played sound better. This makes the music you play conform to the music volume set in system, which is now controlled by the volume rocker.
Source:
http://developer.android.com/training/managing-audio/volume-playback.html
http://developer.android.com/reference/android/media/MediaPlayer.html#setAudioStreamType(int)

How to stop Android music player when my app is ringing

I have implemented a VoIP application and everything works fine. When a user is playing a music on his device using Android default music player or any other third party apps, and my app starts to ring or a message is received, my app's ringtone is not played and the music player is still playing and this situation keeps happening even in incall and call termination states.
How do I detect when a music player (default player for this stage) is playing a music and how to pause and resume it on my app's ringing and call termination states?
An application should request Audio Focus when it needs to utilize the audio output, either with music or notification sounds. Once it has the priority of Audio Focus, it can use the sound output freely, while listening for focus changes. If Audio Focus loses the focus, it should immediately either kill the audio or lower it to a quiet level and only resume loud playback when it receives focus again.
If an application needs to output music, Audio Focus should be requested. The method requestAudioFocus() should be called from the AudioManager.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus( this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// could not get audio focus.
}
Take a look on Handling Audio Sound from Overlapping with VOIP call

Android Play Sound even if Media Volume is 0

I want to play music with AudioTrack even if the media volume of the device is 0.
In my app, I want to have a SeekBar for the media volume (maybe if other music runs in the background) and one other SeekBar for the music of my app.
Until now, I can change the media volume. But then the music of my app is also silent..
How can I do that?
I think there is Six Different Streams of Sound in Android for Playing Different Types of Sounds
The first step to creating a predictable audio experience is understanding which audio stream your app will use.
Android maintains a separate audio stream for playing
music,
alarms,
notifications,
the incoming call ringer,
system sounds,
in-call volume,
and DTMF tones.
This is done primarily to allow users to control the volume of each stream independently.
Use Hardware Volume Keys to Control Your App’s Audio Volume
By default, pressing the volume controls modify the volume of the active audio stream. If your app isn't currently playing anything, hitting the volume keys adjusts the ringer volume.
If you've got a game or music app, then chances are good that when the user hits the volume keys they want to control the volume of the game or music, even if they’re currently between songs or there’s no music in the current game location.
You may be tempted to try and listen for volume key presses and modify the volume of your audio stream that way. Resist the urge. Android provides the handy setVolumeControlStream() method to direct volume key presses to the audio stream you specify.
Having identified the audio stream your application will be using, you should set it as the volume stream target. You should make this call early in your app’s lifecycle—because you only need to call it once during the activity lifecycle, you should typically call it within the onCreate() method (of the Activity or Fragment that controls your media). This ensures that whenever your app is visible, the volume controls function as the user expects.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
From this point onwards, pressing the volume keys on the device affect the audio stream you specify (in this case “music”) whenever the target activity or fragment is visible.
Thanks for your answers.
Now I use "AudioTrack(AudioManager.STREAM_SYSTEM, (...))" and it's working good.
Maybe it's not the best method but by now, it's working.

Is it possible to control the volume of the default media player in Android from an Android application

I have the following requirement.
I am developing an Android mobile application. A timer has been set for a specific duration for an activity.
I need to play "beep" sound three times when the timer duration is 10 seconds left to complete (i.e. become zero), two times "beep" sound when the timer duration is 5 seconds left to complete & once "beep" sound when the timer completes.
The user may be playing music using the default music player of the Android phone while using the Android mobile application. I need to implement the logic so that when the "beep" sound is being played from the mobile application, I need to first decrease the volume of the default music & then play the "beep" sound & again reset to the original volume after the "beep" sound have been played the required no of times.
I wanted to know, whether this is technically feasible or not.
Yes, you can use AudioManager to change the volume for music. The function [setStreamVolume][2] is what you're looking for. The stream type you're looking for is AudioManager.STREAM_MUSIC
[2]: http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
One way to achieve this is by using the platform's ability to allow your app to request audio focus while allowing other apps to "duck". This allows you to tell the system .. "Hey, I'm going to play something, tell other apps also playing audio that it's ok to continue playing but to lower their volume".
Here is the relevant sample code and note from the Managing Audio Focus section.
When requesting transient audio focus you have an additional option:
whether or not you want to enable "ducking." Normally, when a
well-behaved audio app loses audio focus it immediately silences its
playback. By requesting a transient audio focus that allows ducking
you tell other audio apps that it’s acceptable for them to keep
playing, provided they lower their volume until the focus returns to
them.
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_NOTIFICATION,
//tell them it's ok to duck
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// Start playback.
}
I hope it helps someone.

Categories

Resources