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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:29:49+00:00 2026-05-23T07:29:49+00:00

I hope this isn’t a dumb question, as I am rather new to Android,

  • 0

I hope this isn’t a dumb question, as I am rather new to Android, but I’m having trouble handling the state changes with MediaPlayer. The mediaplayer works fine while on the screen, and it continues to play the sound while the screen isn’t visible, but when I return to the activity and try to stop the mediaplayer, it won’t stop. I don’t see anyway to get a streamID from mp, which I could save and restore on onPause and onResume. There must be something simple I’m missing. Here’s some snippets of what I have so far:

    public class Noise extends Activity {
       public MediaPlayer mp;

     public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layour.main);

        Button startButton = (Button)findViewById(R.id.startButton);
        Button stopButton = (Button)findViewById(R.id.stopButton);

        mp = MediaPlayer.create(this, R.raw.noise);

        startButton.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v){
                 playSoundLoop();
                 }
               });

        stopButton.setOnClickListener(new OnClickListener(){
             @Override
             public void onClick(View v){
                 stopSound();
                 }
               });
       }

    public void playSoundLoop(){
         mp.setLooping(true);
         mp.start();
     }

    public void stopSound(){
         mp.pause();
         //Had originally tried mp.stop, mp.reset, mp.prepare, but that didn't work any better
     }

}

  • 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-05-23T07:29:50+00:00Added an answer on May 23, 2026 at 7:29 am

    I don’t think you’re deallocating the original MediaPlayer instance. So, when you leave the activity and return, onCreate() is called again, and you end up with two instances until memory pressure causes the old one to get released.

    The simple solution is release it when your activity leaves the foreground, eg:

    @Override
        onPause() {
        mp.release();
        super.onPause();
    }
    

    However, if you want your music to continue playing in the background, you need to move the MediaPlayer reference somewhere outside the Activity lifecycle – probably into a service.

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

Sidebar

Related Questions

I'm relatively new to Java, so I hope this isn't a dumb question. I
Alright, I hope this isn't too broad a question but my curiosity got the
I hope this question isn't too open ended, but a nudge in the right
I'm relatively new to web application programming so I hope this question isn't too
I hope this isn't too vague a question, but here goes. I want to
I'm really new at network-programming, so I hope this isn't a complete Newbie-question. I
I'm new to Windows Phone 7 App Programming, so I hope this question isn't
I hope this isn't a duplicate of a question itself, but the search terms
I hope this isn't a stupid question, but after searching around for quite a
I hope this isn't a dumb question. You can set a default value for

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.