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

  • Home
  • SEARCH
  • 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 8334429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:20:57+00:00 2026-06-09T03:20:57+00:00

I am creating a music player app. I want: When the user plays a

  • 0

I am creating a music player app.

I want:

  1. When the user plays a song using my app, then the default music player should get stopped(is playing)
  2. And if some other application starts playing when my app is already playing then mine should get stopped.

I am able to work out with the 1st point using Audio Focus.
But for the 2nd point, i.e. Handling AUDIOFOCUS_LOSS, i’m facing some issues.

Problem:
I’ve followed the documentation but how should I register my music player service to detect that AudioFocus has been lost.

For 1st Point, here is the code:

if (reqAudioFocus()) {
    mPlayer.start();
}

private boolean reqAudioFocus() {
    boolean gotFocus = false;
    int audioFocus = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (audioFocus == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        gotFocus = true;
    } else {
        gotFocus = false;
    }
    return gotFocus;
}

For 2nd point my code so far:

//Class implements OnAudioFocusChangeListener

@Override
public void onAudioFocusChange(int focusChange) {
    /*
     * TODO : Call stopService/onDestroy() method. Pause the track; Change
     * the Play/Pause icon; save the track postion for seekTo() method.
     */
    if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
        // TODO: Pause playback
        if (mPlayer.isPlaying()) {
            mPlayer.pause();
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // TODO: Resume Playback
        if (!mPlayer.isPlaying()) {
            mPlayer.start();
        }
    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
        // TODO: stop playback.
        // am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
        am.abandonAudioFocus(afChangeListener);
    }

}

Now how to make my application call onAudioFocusChange() when app loses audio focus.

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-09T03:20:58+00:00Added an answer on June 9, 2026 at 3:20 am

    The first argument to requestAudioFocus is an OnAudioFocusChangeListener instance. You’re passing null at the moment. Assuming you’ve implemented reqAudioFocus and onAudioFocusChanged on the same class, passing this instead of null should be enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a music player app. If in background the default music player is
I'm, creating a player app. I've implemented 'remoteControlReceivedWithEvent:' method and could change playing state
I am creating player application, which is playing audio streams through internet. I want
I'm creating an online music player using php and javascript and I can't figure
So in my new music app game I'm creating, I'm allowing the user to
When any music player playing a song, clicked on its notification it will show
I was thinking of creating a music player with waveform like in zippyshare like
I'm working on an ios app with uitabviewcontroller that plays some music. I don't
I am creating a little JavaScript music player that sits on the btottom of
I'm busy creating a mp3 player (for a music store). Everything works perfectly. Some

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.