Detect headset press on android - Media Button

147 views
Skip to first unread message

I.R.B.A.

unread,
Aug 18, 2019, 12:23:19 PM8/18/19
to Kivy users support
My goal is to intercept media button press on Kivy but nothing I do actually works, I have no experience with java so any help will be appreciated as I am willing to learn. Thank you


from kivy.core.window import Window
from android.broadcast import BroadcastReceiver
from jnius import autoclass
KeyEvent = autoclass("android.view.KeyEvent")


class TestApp(App):


   
def build(self):
       
self.br = BroadcastReceiver(
           
self.on_broadcast, actions=['media_button'])
       
self.br.start()


   
def on_broadcast(self, context, intent):
       
event = intent.getParcelableExtra(intent.EXTRA_KEY_EVENT)
       
if event == KeyEvent.ACTION_UP and KeyEvent.KEYCODE_MEDIA_PLAY:
           
print("Media button pressed")


       
# extras = intent.getExtras().getString("EXTRA_KEY_EVENT")


   
"""
    def on_start(self):
        win = Window
        win.bind(on_keyboard=self.my_key_handler)


    def my_key_handler(self, window, keycode1, keycode2, text, modifiers):
        if keycode1 in [KeyEvent.KEYCODE_HEADSETHOOK, KeyEvent.KEYCODE_MUSIC, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE]:
            print("
Media button pressed")


            return True
        return False
    """



   
def on_pause(self):
       
self.br.stop()
       
return True


   
def on_resume(self):
       
self.br.start()




I.R.B.A.

unread,
Aug 22, 2019, 6:41:38 PM8/22/19
to Kivy users support
UPDATE:
Intent = autoclass("android.content.Intent")
   
def on_broadcast(self, context, intent):
       
event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)

       
if event == KeyEvent.ACTION_UP and KeyEvent.KEYCODE_MEDIA_PLAY:
           
print("Media button pressed")

intent-filters.xml
<intent-filter android:priority="2147483647" >
   
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>




Reply all
Reply to author
Forward
0 new messages