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 9029121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:10:26+00:00 2026-06-16T07:10:26+00:00

I am using the Android MediaPlayer class to play an mp3 file from a

  • 0

I am using the Android MediaPlayer class to play an mp3 file from a running Service class. I wanted to stop the music player from playing if the user stops viewing any of the five Activities.

I tried it by using the code here to send a broadcast intent to the Service that holds the MediaPlayer. That way when the broadcast receiver inside of the Service receives this intent it will call the player.pause() method to stop the music from playing.

However it is not working. I started the player while viewing one of the activities, then next I left the application and viewed some other app like the Android calendar app. The music does not stop and keeps playing.

Any ideas on how to fix this problem?

   public class Monitor extends Application {

  @Override
   public void onCreate(){
   super.onCreate();

   if(activityVisible==false){
        Intent intent = new Intent();
        intent.setAction("com.sample.test");
        sendBroadcast(intent);
   }

   }


 public static boolean isActivityVisible() {
        return activityVisible;
      }  

      public static void activityResumed() {
        activityVisible = true;
      }

      public static void activityPaused() {
        activityVisible = false;

      }

      private static boolean activityVisible;

   }

And here is what I put in each of the other 5 activities;

  @Override
  protected void onResume() {
    super.onResume();
    Monitior.activityResumed();
  }

  @Override
  protected void onPause() {
     super.onPause();
    Monitor.activityPaused();
  }
  • 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-16T07:10:27+00:00Added an answer on June 16, 2026 at 7:10 am

    onCreate in the Application class is not called when each of your activity’s is created. Is this what you intended?

    So your Monitor will change the activityVisible to false, but from the code you have posted nothing ever acts upon this boolean.

    One solution is to create a MonitorActivity and have all your Activitys extends this. Then in the onCreate of the MonitorActivity you will send your broadcast to start the service and in the onPause stop the service. Yes this will pause between activitys so you could have a 3 second fuse of whether the service should actually stop.

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

Sidebar

Related Questions

Using the native Android MediaPlayer class I am able to play Shoutcast streams. Furthermore,
I'm trying to run a mp3 file using android media player. I'm using this
I am using media player to play a mp3 file.But when i run it,i
I am using android media player class for playing notification sound in my android
In my app, I am using media player in service for playing a music
I'm using the Android's MediaPlayer class to play some remote resources. I would like
I'm using the built in MediaPlayer class in Android 2.1 to play audio &
I am using the Android MediaPlayer class and trying to write a testcase which
I am trying to play continuous stream using android mediaplayer.but am not able to
I am using android MediaPlayer class and one of the activities of this app

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.