Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:55:54+00:00 2026-06-10T02:55:54+00:00

The title makes this sound much simpler than it is.. I’m trying to broadcast

  • 0

The title makes this sound much simpler than it is.. I’m trying to broadcast an intent that will pause most music players.

I know I can use create a KeyEvent for that will broadcast KEYCODE_MEDIA_PLAY_PAUSE with this:

    long eventTime = SystemClock.uptimeMillis(); 
    Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
    KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
    downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); 
    ctx.sendOrderedBroadcast(downIntent, null);

And that works. It will pause the stock media player how I want it to, along with most other music players that support headphone play/pause buttons. But for some reason, it only works once with the stock music player. I can press the button to call this, and it’ll pause music. If I start playing it again, and hit my pause button again, it doesn’t work. Once I reboot the device, it’ll work once again. But then, with Pandora, it works consistently as it should.

I thought I might be able to work around that and just pause without using a KeyEvent. I tried this with AudioManager and another intent:

    AudioManager mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);    
    if (mAudioManager.isMusicActive()) {
        Intent mediaIntent = new Intent("com.android.music.musicservicecommand");
        mediaIntent.putExtra("command", "pause");
        ctx.sendBroadcast(mediaIntent);
    }

And this also works, and it works consistently. But it’s inherently fragile, because by sending the intent directly to the Android music player, that’s the only player it’ll work with. No control of Pandora, Last.FM, etc. and I need it to work with most music players.

I’m just not sure where to go next. I’m not picky about what kind of solution I’m given as long as it works. If you can help me make the KeyEvent work, fantastic, if you have some totally different solution, fantastic. I just want it to work! Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:55:54+00:00Added an answer on June 10, 2026 at 2:55 am

    To pause system wide audio, you don’t start an intent but rather request audio focus from the system.

    So when you wish to pause everything do the following (suggested in onCreate):

    //The Variables we'll need to create
    AudioManager am;
    OnAudioFocusChangeListener af;
    
    //I do nothing with this listener, but it's required for the next step.
    af = new OnAudioFocusChangeListener() {
    
          public void onAudioFocusChange(int focusChange) {
              if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
                  // Lower the volume
              } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
                  // Raise it back to normal
              }    
          }
    
    }; 
    
    //Do the actual pause
    am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    int request = am.requestAudioFocus(af,
        AudioManager.STREAM_MUSIC,
        AudioManager.AUDIOFOCUS_GAIN);
    

    But don’t forget to let go of focus using:

    am.abandonAudioFocus(af);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the title makes this sound very easy, but I have a For
Not sure if the title makes much sense, but here is what I'm trying
I am trying to make a title appear and disappear over a thumbnail. This
I hope the title makes sense. I have a set of items that I
I know this question will sound like a from the past thing but I
Hopefully that title makes sense... Let's say I have an employee table: ID |
Sorry for long winded title, this makes a lot more sense with an example.
Ok, the title may sound a little weird, but I think that kinda describe
ok not as simple as title may make it sound. I tried this in
Not sure if the title makes sense sorry... basically I'm generating Word documents that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.