Until now, I was setting my MediaPlayer volume by setting the stream volume. I don't want to do that anymore because it messes with user settings. I now take the value from a SeekBar (0 to 100) and do valueFromSeekBar / 100 to get a float between 0 and 1 to use in MediaPlayer.setVolume(float, float).
The problem is that the volume level doesn't seem to change. Here is how I set up the MediaPlayer:
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
float alarmVolume = AudioUtils.getMediaPlayerScaledVolume(100, alarm.volume);
if(NetworkUtils.isInCall(context)) {
alarmVolume = IN_CALL_VOLUME;
}
mediaPlayer.setVolume(alarmVolume, alarmVolume); //I've even tried hardcoding 0.1f
No matter what I do, it seems like the value I put in MediaPlayer.setVolume gets ignored, and the volume of the stream (in this case the alarm stream) gets used instead. It's most noticeable when the stream volume is set to max, and I play two audio files, one with MediaPlayer.setVolume(1f, 1f) and the other with MediaPlayer.setVolume(0.01f, 0.01f). They are almost indistinguishable from one another. I need a way for my users to be able to position the SeekBar at 1 and get a barely audible sound, or at 100 and have the max sound. Is this possible or am I gonna have to go back to messing with streams?
Set volume:
it will set maximum value(100) to Alarm Stream.
amanager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
amanager.setStreamVolume(AudioManager.STREAM_ALARM, amanager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_PLAY_SOUND);
Can it be you have two objects "player" and "mediaPlayer"? Here I just used that API, and it works as was to be expected.
Related
I imagine the answer to this question must be something quite typical. The point is that I have some buttons that play sounds when you click on them. I want to control the sound volume. How do I control the sound volume? I am using the following code:
private fun playSound() {
val sound = getSystemService(Context.AUDIO_SERVICE) as AudioManager
sound.playSoundEffect(AudioManager.FX_KEY_CLICK, 1f)
}
https://developer.android.com/reference/android/media/AudioManager#playSoundEffect(int,%20float)
volume float: Sound effect volume. The volume value is a raw scalar so UI controls should be scaled logarithmically. If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. NOTE: This version is for applications that have their own settings panel for enabling and controlling volume.
So you need to adjust that second parameter. I haven't used it, but it says that's a scalar so I'd imagine 1 is full volume? It doesn't mention any other value as a max constant or anything. -1 gives you a sound a little quieter than the current media volume setting, if that's convenient. You'll need to play around with different values between 0 and 1 (I assume!) and see what you need
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 would like to play an audio file that starts on the left speaker and then switches to the right speaker.
I have tried doing something like this:
MediaPlayer mp = new MediaPlayer();
// Setup audio file
mp.start();
mp.setVolume(1.0F, 0F);
// Delay a second or two (I actually use a Handler and the postDelayed method)
mp.setVolume(0F, 1.0F);
but the sound comes through on both speakers the whole time.
How can I play audio in Android with either the left or right speaker muted (or at reduced volume)?
EDIT:
I got the correct behavior for a while while I was testing my app, but then it returned to what I described above with the exact same code base. Based on this, is there anything else I could check to find out what's going on?
One option would be
Start mediaplayer with setVolume(1.0F, 0F);
When you want to switch to other speaker, get current position of media player by using getCurrentPosition() method.
Then stop media player.
Then again start with setVolume(0F,1.0F);
Seek to the positin you got in 2nd step using seekTo() method
Done.
Overhead:This method may cause you some delay
It looks like you are doing it correctly according to the Android API http://developer.android.com/reference/android/media/MediaPlayer.html
public void setVolume (float leftVolume, float rightVolume)
Sets the volume on this player. This API is recommended for balancing the output of
audio streams within an application. Unless you are writing an application to control
user settings, this API should be used in preference to setStreamVolume(int, int, int)
which sets the volume of ALL streams of a particular type. Note that the passed volume
values are raw scalars in range 0.0 to 1.0. UI controls should be scaled logarithmically.
Parameters
leftVolume left volume scalar
rightVolume right volume scalar
My best advice is to try 0.0F instead of just 0F and then maybe trying to set the volume before you start playing the track then transition while it's playing.
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 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.