Monali
Monali

Reputation: 1966

how to handle media button to stop a song?

I have one service in which I am playing a song. Now I want to stop the song when I press volumn up and down button.

I used broadcast receiver also with the following code but it doesn't help me

 if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
 {
      player.stop();    
 }

Upvotes: 1

Views: 2212

Answers (2)

Kantesh
Kantesh

Reputation: 875

Intent you handled 'll not be fired when you press volume buttons of device but remote buttons like earphone buttons. I think there is no intent action associated with device volume buttons if those are not implemented in framework or hardware level. Please have look at it:

An example of the handling of media button intents I think you can catch these key events in onKeyDown() in an activity.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006614

Now I want to stop the song when i press volumn up and down button.

The "volumn up and down button" is not the media button. There is no broadcast Intent for the volume rocker, so your service will not be able to find out about such presses on its own. An activity can watch for volume button KeyEvents via onKeyDown().

Upvotes: -1

Related Questions