Setting The Initial Volume To The Phones Ring Volume
im trying to make it so when the user opens the app it sets the volume of the music to whatever they have their phones ringer volume at. This is my code so far but im not exactly s
Solution 1:
Looks like audio.setStreamVolume is what you want, but pass in STREAM_MUSIC instead of STREAM_RING.
Note: the music volume and ringer volume are likely to have different maximum values, so you will need to normalize them. Use getStreamMaxVolume for that.
I have not done this before, and I haven't compiled this, but the code should look something like this
AudioManageraudio= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Get the current ringer volume as a percentage of the max ringer volume.intcurrentVolume= audio.getStreamVolume(AudioManager.STREAM_RING);
intmaxRingerVolume= audio.getStreamMaxVolume(AudioManager.STREAM_RING);
doubleproportion= currentVolume/(double)maxRingerVolume;
// Calculate a desired music volume as that same percentage of the max music volume.intmaxMusicVolume= audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
intdesiredMusicVolume= (int)(proportion * maxMusicVolume);
// Set the music stream volume.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, desiredMusicVolume, 0/*flags*/);
Post a Comment for "Setting The Initial Volume To The Phones Ring Volume"