+ Reply to Thread
Results 1 to 2 of 2

Thread: Mediaplayer crash

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    14

    Mediaplayer crash

    I have created an activity with a button and the button plays a sound when it is clicked , here is the code for that button :
    Code:
    if (Mediapx != null)
    {
        Mediapx.release();
    }
    Mediapx = MediaPlayer.create(this, R.raw.match);
    Mediapx.start();
    Mediapx is a field in the activity:
    Code:
    public class Game extends Activity iMediapxlements OnClickListener {
    MediaPlayer Mediapx;
    //...
    Application runs fine , but sometime when i click the button it crash and gives that error :
    Code:
    bufferCount (4) is too small and increased to 12
    not enough memory for AudioTrack size=58173
    AudioTrack (0xdbb0, size=1051392)

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    35
    If you are attaching a sound effect to a button, MediaPlayer in general is far too heavyweight for this operation.
    You're getting unnecessary latency each time just to load up the sound data into memory.
    You should look at using SoundPool instead.
    Code:
    MediaPlayer Mediapx;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //Other init code
    
        //Create the player this way so it doesn't auto-prepare
        Mediapx = new MediaPlayer();
        AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.match);
        Mediapx.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
    }
    
    public void onDestroy() {
        super.onDestroy();
        //Release it only when no longer needed
        Mediapx.release();
        Mediapx = null;
    }
    
    public void onButtonClick(View v) {
        if (Mediapx.isPlaying()) {
            Mediapx.stop();
        }
        //Play the sound
        Mediapx.prepare();
        Mediapx.start();
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts