Increase MediaPlayer volume beyond 100% - android

Below code is working but not increasing the media player volume higher than the default max volume.Please help
AudioManager am =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(
AudioManager.STREAM_MUSIC,
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
0);

The MediaPlayer class's setVolume() method only accepts scalars in the range [0.0, 1.0], but the classes deriving from AudioEffect can be used to amplify the MediaPlayer's audio session.
For example, LoudnessEnhancer amplifies samples by a gain specified in millibels (i.e. hundredths of decibels):
MediaPlayer player = new MediaPlayer();
player.setDataSource("https://www.example.org/song.mp3");
player.prepare();
// Increase amplitude by 20%.
double audioPct = 1.2;
int gainmB = (int) Math.round(Math.log10(audioPct) * 2000);
LoudnessEnhancer enhancer = new LoudnessEnhancer(player.getAudioSessionId());
enhancer.setTargetGain(gainmB);
It's unclear from the documentation, but it appeared to me that LoudnessEnhancer doesn't work properly with negative gains, so you may still need to use MediaPlayer's setVolume() method if you want to decrease the volume.
DynamicsProcessing provides multiple stages across multiple channels, including an input gain stage.

For increasing the volume of the device beyond the system volume u have to go in engineers mode.for that save below code.and paste it in number entering box In calling option it will directly redirect you to the engineers mode
*#*#3646633#*#*
By this you can access the system settings one thing make sure that don't use this without care it may affect your system performance.

Related

how to control the sound volume of my own android app

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

Android: why MediaPlayer plays the same sound louder than SoundPool?

You can play a sound with MediaPlayer or SoundPool on Android.
Two of our Android devices play the same sound much louder when MediaPlayer is used.
On the third device, the volume sounds the same.
Do you know why?
Can I make SoundPool to play sounds as loud as MediaPlayer?
Devices in question:
Tablet LG-V400 (Android 4.4 Kit Kat)
Phone Sony Xperia L (Android 4.2.2)
Code: play mp3 sound with SoundPool
private void playSoundPool() {
int MAX_STREAMS = 2;
SoundPool soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
int loop = 0;
int priority = 0;
float rate = 1.f;
soundPool.play(soundId, 1, 1, priority, loop, rate);
}
});
soundPool.load(this, R.raw.test, 1);
}
Code: play mp3 sound with MediaPlayer
private void playMediaPlayer() {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test);
mediaPlayer.setVolume(1, 1);
mediaPlayer.start();
}
You are welcome to download test project.
Possible Solution:
You need to create AudioManager for getting, changing the global media volume set by user in phone and changing the volume for our own app independently by changing stream volume in soundPool.
AudioManager mgr = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / AudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
Note:
if I set 0.5 for volume in soundpool, the actual volume will be always half of the global one. Very easy to reproduce:
set global media volume in phone settings to max
set volume in activity using soundpool.play to 0.5 - play sound
set volume in soundpool.play to 1 - play sound, it will be two times
louder
So volume passed to SoundPool.play method really a multiplier to the global volume!
**So If you want to play a sound at the current volume setting just pass "1" as the volume or change it as per the requirement **
may be media player class using global media volume for playing the audio file. you are using hard-coded soundPool.play(soundId, 1, 1, priority, loop, rate); values !
we need try with
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test);
mediaPlayer.setVolume(streamVolume,streamVolume);
mediaPlayer.start();
Some more Texts :
SoundPool:
SoundPool is designed for short clips which can be kept in memory
decompressed for quick access, this is best suited for sound effects in apps or games. Using this method with soundboards is a bad
idea as you will be loading lots of “medium” sized sounds into the
memory and you may exceed your limit (16Mb) and get an
OutOfMemoryException. SoundPool load music files using a separate
thread, the operation of the main thread does not block the UI.
it can be used to play short sound clips say like gun shots during
a game (something like less than 3 seconds long). A nice feature that
sound pool comes with is that you can play many sounds simultaneously
which happens a lot when you think of a game. So you first build a
Sound Pool object to load all media files.
MediaPlayer:"
MediaPlayer is designed for longer sound files or streams, this is
best suited for music files or larger files. The files will be loaded
from disk each time create is called, this will save on memory space
but introduce a small delay (not really noticeable).
Ref: SO
Mechanism difference is there, as you can say that due to compression and decompression the sound quality and volume could go below the actual one, and when you compare it with MediaPlayer. So it is better to use MediaPlayer for better sound quality and volume in your case.
SoundPool
SoundPool is designed for short clips which can be kept in memory decompressed for quick access, this is best suited for sound effects in apps or games. Using this method with soundboards is a bad idea as you will be loading lots of “medium” sized sounds into the memory and you may exceed your limit (16Mb) and get an OutOfMemoryException.
MediaPlayer
MediaPlayer is designed for longer sound files or streams, this is best suited for music files or larger files. The files will be loaded from disk each time create is called, this will save on memory space but introduce a small delay (not really noticeable).

Get media player to play first on right speaker and then on left speaker

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.

Android Media Player setVolume Issues

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.

How to play media file

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.

Categories

Resources