How do you get/set media volume (not ringtone volume) in Android?

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; } }

setStreamMute never unmutes

Apperantly, there is a bug in these Android versions; Tested for versions 2.2 and 2.3.3 and the bug exists. Looks like, if you call setStreamMute on an AudioManager object: AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); am.setStreamMute(…., true); and you lose your reference, then get a new reference: am = null; am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); No matter how many … Read more

How to mute audio in headset but let it play on speaker programmatically?

AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_CALL); am.setSpeakerphoneOn(true); And then play the sound through the AudioManager.STREAM_SYSTEM stream. When the sound’s finished playing be sure to return the audio manager to its previous state or it’ll stay on loudspeaker!!

In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent)

Thanks for your answers, here is a little more detail. To be able to set ringer mode to silent, you must ask permission to access notification policy (like @ucsunil said). <uses-permission android:name=”android.permission.ACCESS_NOTIFICATION_POLICY” /> Then, check if you have this permission. If you do not, open the settings for “Do Not Disturb access” for your app: … Read more

How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)

How about this call: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG which I found at Droid Incredible Headphones Detection ? The updated code I see in your question now isn’t enough. That broadcast happens when the plugged state changes, and sometimes when it doesn’t, according to Intent.ACTION_HEADSET_PLUG is received when activity starts so I would write: package com.example.testmbr; import android.os.Bundle; import … Read more

Using SeekBar to Control Volume in android?

Please look at below code . It solves your problem. import android.app.Activity; import android.content.Context; import android.media.AudioManager; import android.os.Bundle; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class TestExample extends Activity { /** Called when the activity is first created. */ private SeekBar volumeSeekbar = null; private AudioManager audioManager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setVolumeControlStream(AudioManager.STREAM_MUSIC); … Read more

Change Media volume in Android?

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 … Read more