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.
Related
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.
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)
I play a song in background, and the screen is off now. I press the volume key, the volume of the music stream changes. How can it happen? I have searched for a long time for a solution about how to detect the volume keys press event when screen off, but I don't know how to solve the problem so far.
you need a BroadcastReceiver that listens to the according broadcasts. see a similar question here.
If you wish to control the volume of audio played through your app, make use of setVolumeControlStream() method. It directs volume key presses to the audio stream you specify.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
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.
Is it possible to register a listener to listen for changes in the volume level of the music stream in Android?
I'm displaying the actual volume in a SeekBar and I would like to change the seekbar if the user changes the volume with the hardware volume keys. At the moment the correct volume is displayed until the user changes the volume with hardware keys.
This is not a perfect answer but a hack:
android.media.VOLUME_CHANGED_ACTION
I found above action in native logs while changing volume via hard volume key.
01-25 16:11:24.015: DEBUG/VolumePanel(189): onVolumeChanged(streamType: 2, flags: 4)
01-25 16:11:24.015: DEBUG/BluetoothA2dpService(189): Received intent with action: android.media.VOLUME_CHANGED_ACTION
so go ahead and register BroadcastReceiver with action "android.media.VOLUME_CHANGED_ACTION" if you don't have any other solution.
Other way of doing is;
Taking over the volume key on Android .
One option would be to use registerMediaButtonEventReceiver and have your application handle the hardware keys. You could adjust the volume seekbar in your app and use AudioManager to adjust the volume.
Another possibility would be to create a service in your app that runs in the background and periodically checks the volume and adjusts your seekbar accordingly.
You can display the volume by accessing the current volume from AudioManger if your app is currently in focus. Otherwise, there is no concrete way (official way using api) of doing this.
Android recommends letting the system do all of this for you by calling setVolumeControlStream(). This will bring up a volume seekbar for the audio stream that your app is using whenever the user tries to adjust the volume with their hardware buttons.
"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."
From: http://developer.android.com/training/managing-audio/volume-playback.html
I've faced a similar issue.
The app should show a volume off indicator in case of Media Sound has turned off during the video playback. This approach handles cases when the user changes Media level by swipe too.
So, wrote a simple Rx wrapper for ContentObserver which observes Media Sound level directly ))
Gist link here