MDP
MDP

Reputation: 4267

how to control mediaplayer volume

I created a simple application. In my main activity class i use this to play a sound when a button is clicked

MediaPlayer buttonSound = MediaPlayer.create(this, R.drawable.button_sound); 

public void onClick(View arg0){
                buttonSound.start();
                             }

In my main activity class i have a service that start a background music

 Intent svc=new Intent(this, BackgroundSoundService.class);
 startService(svc);

The problem is that I can't handle them (my background music and my button sound) with the volume buttons of my mobile. If i decrease the volume of my mobile, my application volume doesn't change! what should I do?

Upvotes: 1

Views: 2707

Answers (1)

ridoy
ridoy

Reputation: 6322

You can use AudioManager to control volumn of Media Player.

// Get the AudioManager
AudioManager audioManager =(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
// Set the volume of played media to your choice.
audioManager.setStreamVolume (AudioManager.STREAM_MUSIC,10,0); 

Also can use setVolume (float leftVolume, float rightVolume) for your media player as like as

buttonSound.setVolume(10,10);

Where
Parameters
leftVolume--> left volume scalar
rightVolume--> right volume scalar

Upvotes: 2

Related Questions