MediaPlayer stops playing the sounds - Android
Asked Answered
G

2

8

Here is a simple piano app and it works but there is a problem. After about 20 clicks (sometimes it is exactly 28 clicks) even I click the buttons it doesn't play any sound. The app doesnt crash or doesn't warn me about anything. It is just nothing . There is no sound. Do you have any idea?

package com.example.playaudio;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


    public class MainActivity extends Activity implements OnClickListener{
        private MediaPlayer mp;
        private MediaPlayer mp2;
        private MediaPlayer mp3;
        private MediaPlayer mp4;
        private MediaPlayer mp5;
        private MediaPlayer mp6;
        private MediaPlayer mp7;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            Button button1=(Button)findViewById(R.id.button_1);
            Button button2=(Button)findViewById(R.id.button_2);
            Button button3=(Button)findViewById(R.id.button_3);
            Button button4=(Button)findViewById(R.id.button_4);
            Button button5=(Button)findViewById(R.id.button_5);
            Button button6=(Button)findViewById(R.id.button_6);
            Button button7=(Button)findViewById(R.id.button_7);

            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            button4.setOnClickListener(this);
            button5.setOnClickListener(this);
            button6.setOnClickListener(this);
            button7.setOnClickListener(this);

        }

        public void onClick(View v) {
            int resId;
            int resId2;
            int resId3;
            int resId4;
            int resId5;
            int resId6;
            int resId7;


            switch (v.getId()) {
            case R.id.button_1:
                resId = R.raw.a;
                mp = MediaPlayer.create(this, resId);

                mp.start();
                break;
            case R.id.button_2:
                resId2 = R.raw.b;
                mp2 = MediaPlayer.create(this, resId2);
                mp2.start();
                break;
            case R.id.button_3:
                resId3 = R.raw.c;
                mp3 = MediaPlayer.create(this, resId3);
                mp3.start();
                break;
            case R.id.button_4:
                resId4 = R.raw.d;
                mp4 = MediaPlayer.create(this, resId4);
                mp4.start();
                break;
            case R.id.button_5:
                resId5 = R.raw.e;
                mp5 = MediaPlayer.create(this, resId5);
                mp5.start();
                break;
            case R.id.button_6:
                resId6 = R.raw.f;
                mp6 = MediaPlayer.create(this, resId6);
                mp6.start();
                break;
            case R.id.button_7:
                resId7 = R.raw.p;
                mp7 = MediaPlayer.create(this, resId7);
                mp7.start();
                break;

            }


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }



    }
Griskin answered 14/5, 2014 at 21:13 Comment(2)
The problem solved. Add release methods before mp = MediaPlayer ... if(mp!=null) { mp.release(); } mp = MediaPlayer.create(this, resId); mp.start();Griskin
I have the same kind of issue in my app. I have tried out releasing player before creating. Still, issue exists.Larceny
V
11

Looks like you're creating a new MediaPlayer instance to play each sound. You should either reuse them or clean them up.

From the documentation of the MediaPlayer.create() method:

Convenience method to create a MediaPlayer for a given resource id. On success, prepare() will already have been called and must not be called again.

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.

Vulva answered 14/5, 2014 at 21:32 Comment(3)
Yes i realised and tried that and it worked. I was going to edit my question about the true version and since your answer is right i accepted it. Thank you for your concern :)Griskin
but how can I call release only when the sound is done playing? surely I cant call .start() and then immediately .release() ?Heeltap
@Heeltap You can set a listener to be notified when playback completes.Vulva
C
4

simple way is to add onComplationListener

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer p1) {
        p1.release();
    }
});
Calendra answered 28/1, 2020 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.