I am trying to play sound via media player on STREAM_SYSTEM
using
mediaPlayer.setVolume(1.0f , 1.0f);
or
mediaPlayer.setVolume(0.90f , 0.90f);
But it is playing on vol as per Hardware volume controls in settings.
Basically this line has no effect.
I have added
permissions "Modify..." as well in Manifest
But i saw links in stackoverflow where this API works.
I don't wanna go to API
AudioManager.setStreamVol()....
as it sets stream vol for all apps
What is problem that this setVol API is not working??
Can anybody give a link to working sample??
I faced the same issue. I thought that if I use 0.5f as parameter, I could tell the difference. I tried 0.2f, still no difference. So I thought there was a problem indeed. I finally tried 0.02f just in case, and finally I had a lower volume.
Sample:
replace your
mediaPlayer.setVolume(0.90f , 0.90f);
with
mediaPlayer.setVolume(0.09f , 0.09f);
You must put the volume value between 0.0f and 1.0f. So 1.0f is 100% of the volume. Because of that your line has not effect.
AudioManager audioManager;
int actualVolume;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actualVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, actualVolume + 1, AudioManager.FLAG_SHOW_UI);
The above code is taken from my working project, where it is used to increase the volume of the system (I used it for TV). You can use actualVolume - 1 to lower down the volume.
Related
I have a media player which plays song files. However, no matter how I try to initialize its volume, the only way to change it is manually with the volume buttons. I've tried
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0); // Sets volume to max
and even
mMediaPlayer.setVolume(1, 1);
but none work. I've used this code in the past without problem. I've tried my app on both 5.1.1 and 7.1.1 and no luck. It doesn't matter whether the phone's volume starts in a muted state or not. I checked and maxVolume is non-zero (I've tried just hardcoding numbers too). How can I set the initial volume programmatically? The media player starts playing automatically. (I've tried calling this within the media player's onPrepared listener too in case it made a difference. It doesn't.) I also checked whether the phone volume is "fixed". It's not.
How can I get my player to start playing at max volume (no matter what the phone was set for)?
I found the problem. I had the stream wrong. Instead of STREAM_ALARM it should have been STREAM_MUSIC. The list of streams can be found here:
https://developer.android.com/reference/android/media/AudioManager.html
I need to set media volume to MAX in my app to play a buzzer.
I am trying to do it by using media.setVolume() function but it doesn't seem to work.
I have already tried
mediaPlayer.setVolume(1.0f, 1.0f);
I have also tried
int MAX_VOLUME = 1000;
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 999) / Math.log(MAX_VOLUME)));
mediaPlayer.setVolume(volume, volume);
None of the above worked for me.
Somebody pls help me on how to set media volume to full using MediaPlayer.setVolume(float, float) function.
MediaPlayer.setVolume(float, float) sets the volume of the given MediaPlayer instance. This volume is 1.0f (max) by default. It doesn't change the global media volume which is what I wanted to accomplish originally.
I found a solution that simply sets the global media volume.
Useful Remark: I found many answers on stackoverflow.com for setting max volume level or changing volume, most of them used alarm stream (STREAM_ALARM) to do so. I think using alarm stream would not be a good option if you are playing audio casually.
The global volume of a stream type (music in this case) can be changed using the following code.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Now, play your media object as a Music Stream :
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure that you request MODIFY_AUDIO_SETTINGS permission in your application's manifest.
<uses-permission
android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Note: This only sets Media (Music) Volume to the max. To set other Volumes like Ringer, use STREAM_RING.
Thanks, #MrTristan for your advice, it was really helpful.
make sure you've got MODIFY_AUDIO_SETTINGS set as a permission you request in your app if that's the type of volume you're looking to set.
Note that the passed volume values are raw scalars in range 0.0 to 1.0
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
Why don't you try just using mp.setVolume(1.0, 1.0).
setVolume() doesn't work properly, so you should set stream volume by AudioManager.
This one has a bad effect too and it is that you change the entire stream volume of the user device!
And user doesn't like this, so you should change it back to the default value.
But how?!
Define your AudioManager:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Store current volume and set stream value to its Maximum value:
int currentVolume = Objects.requireNonNull(am).getStreamVolume(AudioManager.STREAM_NOTIFICATION);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
Define AudioAttributes.Builder and set the stream type for it:
AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
audioAttributes.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
Set audio attributes for your MediaPlayer before calling prepare() or prepareAsync():
mediaPlayer.setAudioAttributes(audioAttributes.build());
Finally set a on completion listener for mediaPlayer and change the volume to its default:
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, currentVolume, 0);
}
});
Finish! You could handle the problem. :)
I am preparing an app.My app have one options button with sound on and sound off.For that i used following code,
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Unfortunately, the volume setting for that application would still reflect on the system volume setting for the stream type the application is using. What I would like to know is how to set the volume for that application only without affect the system setting.
Is it possible in android have full control over the volume of the app? Is there any method that could do that? Must I do this with code? please help me
You could use AudioTrack, a lower level sound API. It has an setStereoVolume() which operates independent of the system volume.
I am working in an android app and I need to make an optionMenu with an option to disable/enable the sounds in the app.
Is it possible in android have full control over the volume of the app?
Is there any method that could do that?
Must I do this with code?
Thanks
You can find useful information here. According to that answer, it's not possible. Just don't play the sounds.
By using AudioManager you can get current volume level and max volume level. At the time of button click/ option select you can control the volume level also. Here is the code.I hope this will help you to set volume.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
To Set Volume
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,vol_level, 0);
I am using the following code to play audio file.
I have tested the audio file on Android phone player & its playing quite loud.
When I am trying to play the same audio file from the following code , its very feeble.
Is there any problem with my code ? Can I increase the volume of the media file by changing any value ?
While testing , the volume of the Android device has been put to maximum value.
Kindly provide your inputs/sample code.
Thanks in advance.
public void playAlertSound() {
MediaPlayer player = MediaPlayer.create(this, R.raw.beep);
player.setLooping(false); // Set looping
player.setVolume(0.90f, 0.90f);
// Begin playing selected media
player.start();
// Release media instance to system
player.release();
}
Try player.setVolume(1.0f, 1.0f); instead; or just leave off that line entirely. You can also try scaling up the value past 1.0, although that's not really recommended.
You shouldn't call player.release() immediately. Try calling that in your onPause() or onDestroy() methods instead.
You might try using AudioManager.getStreamMaxVolume() to get the maximum volume and use it:
AudioManager audio =
(AudioManager) Context.getSystemService(Context.AUDIO_SERVICE);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
player.setVolume(max, max);
I'm not sure though if setVolume() expects absolute levels or multipliers from 0.0f to 1.0f. It mentions logarithmic adjustment, so you might try something closer to 1.0f like 0.95f or 1.0f itself.