Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AudioManager.isStreamMuted isn't available?

I'm using android AudioManager to mute and unmute streams like STREAM_SYSTEM and STREAM_NOTIFICATION, muting is straightforward calling setStreamMute but I'm unable to find an isStreamMuted method.

Looking inside source code I can see it exists so the question is 'how can check if a stream is muted?

I'm working on Android 4.0.4

like image 759
dafi Avatar asked Dec 09 '12 16:12

dafi


3 Answers

Reflections are your friend. This worked for me.

try {
    Method m = AudioManager.class.getMethod("isStreamMute", int.class);
    isMuted = (Boolean) m.invoke(am, AudioManager.STREAM_MUSIC);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
    System.out.println(e);
}

Update: The method public boolean isStreamMute (int streamType) is now accessible in API 23 without using reflection.

like image 184
Eric Lange Avatar answered Sep 20 '22 10:09

Eric Lange


The unpleasant answer is that stream mute operates on a per-process basis. So you can safely store the mute state in a static variable somewhere, appropriately wrapped, and track mute state yourself. Speaking from bitter experience. Sometimes you do what you have to do. Why did they do that? That's a more difficult question....

like image 24
Robin Davies Avatar answered Sep 19 '22 10:09

Robin Davies


They stripped the method out of android.jar by using hide:

/**
 * get stream mute state.
 *
 * @hide
 */
public boolean isStreamMute(int streamType) {
like image 38
powder366 Avatar answered Sep 22 '22 10:09

powder366