how to control the sound volume of my own android app - android

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

Related

How do you make a mediaplayer play a sound louder?

In my application a sound is triggered by a button click, but the sound is relatively quiet and requires me to manually increase the volume on my phone to hear. Is there a way to increase the volume so that it get's louder quicker? Our plays louder by default?
Edit: the set volume function didn't have an effect on the overall loudness without physically changing the volume on my phone. Am I doing something wrong or have the wrong values
you can use setVolume method
mediaPlayer.setVolume(0.09f , 0.09f);//0,09f = 90% of volume
public void setVolume (float leftVolume,
float rightVolume)

Increase MediaPlayer volume beyond 100%

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.

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.

Set the volume of an android application

In order for the user to control the volume , my android application has a menu consisting of a slider that provides int values from 0 to 10 , when dragged. After I obtain a value , I must set the volume to the corresponding value chosen by the user , and well , this is the part that I don't know to implement and I 'd like to find about it.
Use the AudioManager class. Essentially the code goes as follows:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(streamType, volume, flags);
The problem is that the volume of the device isn't necessarily mapped from 0 to 10 as you have in your slider. On my emulator, it's from 0 to 7. So what you need to do is getStreamMaxVolume(...) to know what your max is, and then work out your value as a fraction of that. As an example, if your user chooses volume 8 out of 10, that's equivalent to 0.8 * 7 = 5.6, which you should round to 6 out of 7.
The "stream" refers to things like ringer volume, notification volume, music volume, etc. If you want to change the volume of the ringer, you need to make sure all your commands have AudioManager.STREAM_RING as the streamType.

Categories

Resources