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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:52:49+00:00 2026-06-10T23:52:49+00:00

I have an android app with a button that plays a sound. the code

  • 0

I have an android app with a button that plays a sound. the code for playing the sound:

if (mp != null)
{
    mp.release();
}
mp = MediaPlayer.create(this, R.raw.match);
mp.start();

mp is a field in the activity:

public class Game extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//variables:
MediaPlayer mp;
//...

The app runs ok, but after clicking the button about 200 times on the emulator, app crashed and gave me this error https://dl.dropbox.com/u/5488790/error.txt (couldn’t figure how to post it here so it will appear decently)
i am assuming this is because the MediaPlayer object is consuming up too much memory, but isn’t mp.release() supposed to take care of this? What am i doing wrong here?

  • 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-10T23:52:51+00:00Added an answer on June 10, 2026 at 11:52 pm

    It looks like your code should work, but obviously release() isn’t really releasing everything.

    Maybe it’s because you have to reload R.raw.match every time you want to play the sound. If R.raw.match is just a short sound effect, then you might want to consider using SoundPool instead.

    If you use SoundPool you only have to load R.raw.match once which may prevent the memory issues.

    This tutorial has a good example on how to use it: http://www.vogella.com/articles/AndroidMedia/article.html#tutorial_soundpool

    You pretty much just make one instance of SoundPool then load the sound once and play it when you need it.

    Hope this helps!

    Edit

    If you want to use MediaPlayer…

    public class Blah extends Activity implements OnClickListener {
       MediaPlayer mp;
    
       @Override 
       public void onCreate(Bundle b)
       {
          // blah blah
          mp = MediaPlayer.create(R.raw.match);
    
          // blah blah
       }
    
       @Override
       public void onClick(View v)
       {
          if (v.getId() == yourButtonID)
          {
             // play sound from beginning
             mp.seekTo(0);
             mp.start();
          }
       }
    }
    

    This way you only create one instance and whenever you want to play it, you just rewind it to the beginning then play.

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

Sidebar

Related Questions

i have this code package com.tct.soundTouch; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.MotionEvent;
I have an Android app that uses ActionBar Sherlock. I create a menu that
in my android app, I have a button that leads to an activity which
I have Android client app that communicates with server using Socket . On my
I have an Android app, that is using the SIM Toolkit. The application has
I have an android app that I am trying to launch and it gives
I have an android app which has native code. The native code needs to
I have an Android app that has an Application class and I have another
I'm trying to record sound using Android Emulator. I know that this question is
I am designing an android app that needs to play a sound every minute

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.