4

How can I set the MediaPlayer sound volume according to ringtone volume?

I did this method, but doesn't work:

        MediaPlayer player = MediaPlayer.create(MyActivity.this, R.raw.sound);
        AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int currentVolume = audio.getStreamVolume(AudioManager.RINGER_MODE_NORMAL);

        player.setVolume(currentVolume, currentVolume);
zamroni hamim
  • 545
  • 1
  • 6
  • 21
  • What are you actually trying to do? – ianhanniballake Jan 16 '16 at 06:44
  • 1
    Originally, I want to use NotificationManager that will show notification and sound, but only when application is closed. When application is active, I want to use only sound, therefore, I'm trying to use MediaPlayer with volume as same as ringtone volume. – zamroni hamim Jan 16 '16 at 06:54

4 Answers4

9

Instead of adjusting the volume, you should use setAudioStreamType() to set which audio stream you want to play your audio over - this automatically uses the volume of the selected stream. For example, if you want your audio to play at the same volume as a notification would, you could use AudioManager.STREAM_NOTIFICATION:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    That's just what I need to do. I just have to add a method like at the answer : http://stackoverflow.com/questions/10097954/android-mediaplayer-how-am-i-supposed-to-call-setaudiostreamtype-if-mediapla , and it works. Thank you. – zamroni hamim Jan 16 '16 at 09:07
  • 2
    This doesn't seem to cause any errors but I no longer hear any sound (even though my notification volume is way up), and looking at the docs it says this method was deprecated in API level 21... – Michael Aug 11 '19 at 20:34
  • 1
    After messing around found that in order for audio attributes to work correctly, setAudioAttributes must be called before prepare. Using MediaPlayer's create (Context context, int resid, AudioAttributes audioAttributes, int audioSessionId) to pass the audio attributes is the way to go. – Amit Jayant Feb 01 '22 at 10:21
1

Try it:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
Androider
  • 3,833
  • 2
  • 14
  • 24
1

Trying to play on AudioManager.STREAM_NOTIFICATION or AudioManager.STREAM_RING didn't produce any sound. I had to go through AudioManager.STREAM_MUSIC. Using the idea of ssuukk at Android: MediaPlayer setVolume function, I wrote the following code that worked:

AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level1= am.getStreamVolume(AudioManager.STREAM_RING);
int maxVolume=am.getStreamMaxVolume(AudioManager.STREAM_RING);
final MediaPlayer mp1 = MediaPlayer.create(context, R.raw.notification_delivery);
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
float log1=(float)(1-Math.log(maxVolume-volume_level1)/Math.log(maxVolume));
mp1.setVolume(log1,log1);
tsig
  • 148
  • 2
  • 6
1

You can try like below:

 AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
            .setLegacyStreamType(AudioManager.STREAM_RING)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();
    int s = audioManager.generateAudioSessionId();
    mediaPlayer = MediaPlayer.create(context, R.raw.default_ring,audioAttributes,s);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();

You can pass the required stream type in setLegacyStreamType(). if you do like above your media player voulme will follow the stream type volume which is passed to setLegacyStreamType() method in audio attributes.

Rajesh.k
  • 2,327
  • 1
  • 16
  • 19