ssuukk
ssuukk

Reputation: 8410

MediaControllerCompat - java.lang.IllegalArgumentException: Bad direction 3

I'm issuing a simple command to my mediaControllerCompat:

controller.adjustVolume(-1, 0);

Yet my app FCs with...

java.lang.IllegalArgumentException: Bad direction 3
       at android.os.Parcel.readException(Parcel.java:1469)
       at android.os.Parcel.readException(Parcel.java:1419)
       at android.media.IAudioService$Stub$Proxy.adjustStreamVolume(IAudioService.java:1097)
       at android.media.AudioManager.adjustStreamVolume(AudioManager.java:952)
       at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.adjustVolume(MediaSessionCompat.java:1376)
       at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.access$1700(MediaSessionCompat.java:963)
       at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase$MediaSessionStub.adjustVolume(MediaSessionCompat.java:1602)
       at android.support.v4.media.session.MediaControllerCompat$MediaControllerImplBase.adjustVolume(MediaControllerCompat.java:969)
       at android.support.v4.media.session.MediaControllerCompat.adjustVolume(MediaControllerCompat.java:252)
       at pl.qus.xenoamp.NewMainActivity.onKeyDown(NewMainActivity.java:1149)

MainActivity being the caller of mentioned line... What is WRONG?!

Upvotes: 0

Views: 1256

Answers (2)

Abandoned Cart
Abandoned Cart

Reputation: 4676

I came across the same error using API 23 (not the Support Library) and running on an M device. I solved it by listening for the error and calling an API 1 method in its place, when necessary.

try { 
    ...
} catch (IllegalArgumentException e) { 
    audioManager.setStreamVolume(STREAM, VOLUME, FLAGS);
}

Since the Compat interface calls the internal methods for system volume control, you could also instantiate an instance AudioManager if you are not directly declaring one already.

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 200120

This was an internal bug in the Android Support Library has been fixed as of version 23.1.0.

Previous Answer

This is a bug in the support library that affects pre-API 21 devices that use local playback (i.e., have not called setPlaybackToRemote()) - the order of parameters sent to AudioManager.adjustStreamVolume() as per the source code is incorrect - hence why the direction appearing as 3 - the value for STREAM_MUSIC.

You may be able to temporarily work around it by always calling setPlaybackToRemote() on pre-API 21 devices, passing in a VolumeProviderCompat that does correctly call AudioManager.adjustStreamVolume(), but you must also handle the other methods of VolumeProviderCompat such as retrieving the max volume (via getStreamMaxVolume()) and current volume (via getStreamVolume()) as well as setting the volume (via setStreamVolume()).

Upvotes: 1

Related Questions