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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:05:49+00:00 2026-06-18T02:05:49+00:00

On the MainActivity, I play a background music using MediaPlayer. @Override protected void onCreate(Bundle

  • 0

On the MainActivity, I play a background music using MediaPlayer.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Music.playBGmusic(this, R.raw.main_music);
}

@Override
protected void onResume() {
      super.onResume();
      if ( ! Music.isPlaying(this) )
      {
          Music.playBGmusic(this, R.raw.main_music);
      }
}

In Music.java:

public class Music {
   private static MediaPlayer mMainSound = null;
   public static boolean isPlaying(Context context)
   {
       if ( mMainSound != null )
       {
           return mMainSound.isPlaying();
       }
       return false;
   }

   public static void playBGmusic(Context context, int resource)
   {
       if ( mMainSound != null ) mMainSound.release();
       mMainSound = MediaPlayer.create(context, resource);
       mMainSound.setLooping(true);
       mMainSound.start();
   }

   public static void stopBGmusic(Context context) {
       if (mMainSound != null) {
           mMainSound.stop();
           mMainSound.release();
           mMainSound = null;
       }
   }
}

where mMainSound is a MediaPlayer object. There is a button on my MainActivity, and when it’s clicked, it creates an intent, and startActivity that intent. This new activity calls playBGmusic with another sound. Now when that new activity calls stopBGmusic() and finish() to return to MainActivity, onResume() of MainActivity is called as expected, and it’s supposed to play main_music in the background. What actually happens is that I could only hear less than a second of the music, and it gets cut off. I tried with prepare() before starting MediaPlayer, but still that doesn’t solve the problem. It will be very much appreciated if anyone could clarify this issue for me..

Maybe I should also mention where finish() is called in the new activity, although I’m not sure if that’s of any importance..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch(keyCode)
    {
        case KeyEvent.KEYCODE_BACK:
            showDialog(1);
        break;
    }
    return true;
}  

@Override
protected Dialog onCreateDialog(int id) { //id is ignored
    Dialog dialog = null;
    AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.return_home)
           .setCancelable(true)
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   finish();
               }
           })
           .setNegativeButton(R.string.cancel, null);
    dialog = builder.create();
    return dialog;
}
  • 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-18T02:05:51+00:00Added an answer on June 18, 2026 at 2:05 am

    You should really bind a service to control the MediaPlayer, not have your main activity control it. Instead it should control the service via you binding the service it to your app.

    To put it simply(assuming you want to continuously play music even upon exiting you app):

    When your are playing music you want to run it in the background and have your app control (start/stop/pause) states of your mediaplayer. When you create the media player you play from the background instead of the foreground. This allows you to continuously play music while the user has your service running.

    Communication could be setup using PendingIntent to communicate with your service, through capturing you specific intent filters. I’d recommend go to the Google dev site and do some of the tutorials and try to learn how Intents, intent-filter/actions, and services and also learn the lifecycle of activities and services.

    To do this properly your should allocate some time to research/experiment and learn about android.

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

Sidebar

Related Questions

My code: public class MainActivity extends GraphicsActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
I am using a Service to play background Music. The problem is that the
private void playSound(){ Log.d(TAG,Play SOund); new Thread(new Runnable() { public void run() { MediaPlayer
I'm creating a mediaplayer service to play a media file from internet, like this:
I have an android app where in my Main activity I can play music
I have an activity ( MainActivity.java ) in which content view is like this
This is my static inner class for creating an AlertDialog inside my MainActivity class:
I have this situation where i have to start an activity from my mainActivity.
Now I have a mp3 player that can play music, and I'm wondering should
I am using media player to play a mp3 file.But when i run it,i

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.