Android app not recognising volume key presses when showing android.widget.MediaController -
i cannot android app (running on android 6.0) respond volume key presses when android.widget.mediacontroller showing.
the media controller shown fragment when button in fragment tapped.
the activity has loaded fragment has following code in oncreate:
audiomanager audiomanager = (audiomanager) getsystemservice(context.audio_service); this.setvolumecontrolstream(audiomanager.stream_music);
when media controller not showing, volume key presses adjust volume , volume adjustment impact on volume sound played media controller.
however, when media controller shown visible in app, volume keys ignored. have overridden onkeydown method of activity detect when key presses recognised, , recognised when media controller not visible.
@override public boolean onkeydown(int keycode, keyevent event) { switch (keycode) { case keyevent.keycode_volume_up: log.i(tag, "increased volume"); getaudiomanager().adjuststreamvolume(audiomanager.stream_music, audiomanager.adjust_raise, audiomanager.flag_show_ui); return true; case keyevent.keycode_volume_down: log.i(tag, "decreased volume"); getaudiomanager().adjuststreamvolume(audiomanager.stream_music, audiomanager.adjust_lower, audiomanager.flag_show_ui); return true; default: return false; } }
users tend want adjust volumes when sound being heard , when cannot adjust volumes, way things working. how can media controller behaviour changed recognises volume key presses?
had issue yesterday , stumbled upon question. know it's rather old , you've found way around thought should post answer in case else finds way here.
your initial idea helped lot instead of overriding handler keyevents
activity did mediacontroller
.
what did override public boolean dispatchkeyevent(keyevent event)
, initialise mediacontroller
object this:
mediacontrols = new mediacontroller(this) { @override public boolean dispatchkeyevent(keyevent event) { int action = event.getaction(); int keycode = event.getkeycode(); switch (keycode) { case keyevent.keycode_volume_up: if (action == keyevent.action_down) { audiomanager.adjustvolume(audiomanager.adjust_raise, audiomanager.flag_show_ui); } break; case keyevent.keycode_volume_down: if (action == keyevent.action_down) { audiomanager.adjustvolume(audiomanager.adjust_lower, audiomanager.flag_show_ui); } break; default: return super.dispatchkeyevent(event); } return true; } };
audiomanager
private member in activity
, created same way in question above.
hope someone.
have productive day!
Comments
Post a Comment