Change Media volume in Android? - android

Can I change the media volume? and how? I used this so far:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
But have a seekbar and want to change the media volume, not ring volume.
So can someone show me how to just change the media volume at onCreate() and I fix the seekbar later.

The right method to use would be setStreamVolume on your AudioManager. It could looks like this
AudioManager audioManager =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
[int value],
[if desired a flag]);
An example use of the flag is to get the beep when setting the volume so the user can hear the outcome. The flag for that would be AudioManager.FLAG_PLAY_SOUND.
You could use AudioManager.FLAG_SHOW_UI if you don't want to play a sound but display a toast with the current value. The use has to get a feedback tho. Doesn't matter if it is audible or visual.
To get the maximal valid value for the given stream you just call getStreamMaxVolume() on the AudioManager and get an integer back which represents ... well the maximal valid value for the volume.

private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
// return false;
// Update based on #Rene comment below:
return super.onKeyDown(keyCode, event);
}
}

You can use the following code to handle Volume using a SeekBar:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
SeekBar sbVolumeBooster = (SeekBar) findViewById(R.id.sbVolumeBooster);
sbVolumeBooster.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
sbVolumeBooster.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
sbVolumeBooster.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0); // 0 can also be changed to AudioManager.FLAG_PLAY_SOUND
}
});

Giving a 0 - in the flags avoids getting a visual and audio indicator .
That's good when you implement your own audio bar and indicator and you don't want android to add anything.

Use adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, flags);
http://developer.android.com/reference/android/media/AudioManager.html#adjustStreamVolume(int, int, int)

Related

How to set Videoview Volume Programmatically?

I have 4 Videoview which is created dynamically. I want to set a custom volume for only one video which is coming from the server, the remaining three videos will be mute.
I want to set a custom volume for particular videos I tried below code it's not working
vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
//mp.setVolume(100f, 100f);
//mp.setLooping(true);
vid1.enableSound(20,mp); //here i will set music sound dynamically
vid1.start();
playingvideo1 = true;
}
});
//startTimeForContent = dateFormatForContent.format(new Date());
vid1.setOnErrorListener(mOnErrorListener2);
playBackfunction1();
}
public void enableSound(int sound, MediaPlayer mp){
Float f = Float.valueOf(sound);
Log.e("checkingsounds","&&&&& "+f);
mp.setVolume(f,f);
mp.setLooping(true);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND);
}
When I give volume 0 it is working...but when change 10,20,30 video is playing full sound....
I already research below:
sound volume not working on Android
Using SeekBar to Control Volume in android?
How to increase and decrease volume in android
After researching some hours I got a solution
You need to set Volume 0 to 15
vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
//mp.setVolume(100f, 100f);
//mp.setLooping(true);
vid1.enableSound(10,mp); //set Volume 0 to 15.
vid1.start();
playingvideo1 = true;
}
});
//startTimeForContent = dateFormatForContent.format(new Date());
vid1.setOnErrorListener(mOnErrorListener2);
playBackfunction1();
}
public void enableSound(int sound, MediaPlayer mp){
Float f = Float.valueOf(sound);
Log.e("checkingsounds","&&&&& "+f);
mp.setVolume(f,f);
mp.setLooping(true);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //Max Volume 15
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //this will return current volume.
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND); //here you can set custom volume.
}

Problems with audio in onTouchListener and mediaplayer.setLooping

im trying to do a Soundboard for a school proyect, but I have problems when I pulse the buttons, If I pulse a button twice, the sound does not reproduce again, and, even if the condition fullfills, the audio doesnt loop. I have different buttons, and all of they call a method (each one with different parameters on the call
btn1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
reproducir(mp1, motionEvent, btn1, colorin);
return false;
}
});
public void reproducir(MediaPlayer mp, MotionEvent motionEvent, Button btn, String colorin ){
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
mp.start();
btn.setBackgroundColor(getResources().getColor(R.color.Blanco));
if(instrumento.getText().equals("piano")) {
mp.setLooping(true);
}
break;
case MotionEvent.ACTION_UP:
mp.stop();
ponerColor(colorin);
}
}
The last line calls to a method to put again the original color of the button, thanks a lot
Your Code is Fine Only One tiny Mistake:
You should replace mp.stop(); with mp.pause();
That's All.
Better to it Like this though:
if (mp.isPlaying()){
mp.pause();
}
....
if (!mp.isPlaying()){
mp.start();
}
...
if (!mp.isLooping()){
mp.setLooping(true);
}
I should also let you know that mp.stop() Stops the media, it's Good, But Stop is only to be used in case You want the Whole MediaPlayer Forget what song is being played and we just wanna change The Audio, reset MEdia player and start another Audio.
Hope you find this useful

Android: Need app's Audio Volume Seekbar to change totally independed of the device volume

I need my in-app volume seekbar to change the volume of the audio in my app only. I need it to be totally separate from the main volume of the device. Everything I find changes the device volume when I change the slider in my app.
I want users to have the option of playing audio in my app at a low volume, but maybe still have their device turned up so phone calls and alarms and such could still be at their normal high volume.
How can I make a seekbar in my app only control audio in my app and not bother other device volumes? Here is the code I am using:
Called in onCreate method:
VolumeControls();
Outside of that:
private void VolumeControls() {
final TextView txtView = (TextView)findViewById(R.id.textView1);
SeekBar seekBar = (SeekBar)findViewById(R.id.sbVolume);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekBar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
txtView.setText(String.valueOf(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC)));
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0){
}
public void onStartTrackingTouch(SeekBar arg0){
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
txtView.setText(String.valueOf(progress));
}
});
}

How to Keep volume fixed in MediaPlayer

I am using MediaPlayer to play a sound and below is my code
mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(0.5f, 0.5f);
mediaPlayer.start();
the MediaPlayer plays the sound in the given volume but the volume can be increased or decreased using the hardware key. I want to know if there is a way to keep the volume for my mediaPlayer fixed no matter what volume the user keeps using the hardware key
Thanks in advance
AFASIK Media Player does not provide any such Listener. You need to override key press events for checking that
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
MediaPlayer provide isPlaying boolean to know either mediaPlayer is in use, you can use that in conjunction with KeyPress events.

How to synchronize Mediaplayer with phone volume?

I noticed that regardless of the volume I set my phone, the sound for a button click in my app remains the same regardless, how can I synchronize it with the phone's system volume? Here's the code format I used
Mediaplayer buttonSoundClick;
buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
buttonClickSound.start();
buttonClickSound = MediaPlayer.create(this, R.raw.button_click_sound);
} private void prepareAsync() {
buttonClickSound.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
#Override
public void onClick(View v) {
//Button Click Sound
buttonClickSound.start();
Android has different volume levels for different stream types.
Override onKeyDown() and add the following code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
After this, try playing around with the stream types to get it right for your app. The stream types available are:
AudioManager.STREAM_MUSIC
AudioManager.STREAM_RING
AudioManager.STREAM_ALARM
AudioManager.STREAM_SYSTEM
AudioManager.STREAM_NOTIFICATION
OR
You could directly set the MediaPlayer stream type by using MediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
Buy, you must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.

Categories

Resources