Android: Why i am not able to play sound on button click after some clicks?

2

I have done like if user click on the Button then the Sound is play. But right now i am not able to listern sound after some clicks. I dont know why ?

Here is the code that i am using to play the sound.

Code:

case R.id.lockView:
        playSound(R.raw.dooropen);
        break;
    }

public void playSound(int resources){
    boolean mStartPlaying = true;
    if (mStartPlaying==true){
        mPlayer = new MediaPlayer();
        Uri uri = Uri.parse("android.resource://com.project.iMystick/" + resources);
        try{
            mPlayer.setDataSource(getApplicationContext(),uri);
            mPlayer.prepare();
            mPlayer.start();
        } 
        catch (IOException e){
            Log.e(LOG_TAG, "prepare() failed");
        }
    } 
    else{
       //   stopPlaying();
        //rePlay.setText("Replay");
        mPlayer.release();
        mPlayer = null;
    }
    mStartPlaying = !mStartPlaying;
}

And After some clicks i got this type of error message in logcat:

Log:

03-27 16:15:02.737: ERROR/MediaPlayer(1057): Error (-19,0)
03-27 16:15:03.858: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
03-27 16:15:03.858: ERROR/AudioFlinger(34): not enough memory for AudioTrack size=32832
03-27 16:15:03.858: DEBUG/MemoryDealer(34):   AudioTrack (0x12abf8, size=1048576)
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     0: 0012ac10 | 0x00000000 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     1: 0012aca0 | 0x00008040 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     2: 0012b5a8 | 0x00010080 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     3: 0012bec8 | 0x000180C0 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     4: 0012c7f0 | 0x00020100 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     5: 0012d1f0 | 0x00028140 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     6: 0012db20 | 0x00030180 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     7: 00136448 | 0x000381C0 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     8: 0013ee00 | 0x00040200 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):     9: 0000de10 | 0x00048240 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    10: 0012b708 | 0x00050280 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    11: 00022c70 | 0x000582C0 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    12: 000234c0 | 0x00060300 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    13: 00115e38 | 0x00068340 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    14: 00117a80 | 0x00070380 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    15: 00118798 | 0x000783C0 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    16: 00042228 | 0x00080400 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    17: 0004bd48 | 0x00088440 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    18: 00055998 | 0x00090480 | 0x00008040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    19: 0005f5f0 | 0x000984C0 | 0x00003040 | A 
03-27 16:15:03.858: DEBUG/MemoryDealer(34):    20: 000712b0 | 0x0009B500 | 0x00008040 | A
03-27 16:15:03.867: ERROR/AudioTrack(34): AudioFlinger could not create track, status: -12
03-27 16:15:03.867: ERROR/AudioSink(34): Unable to create audio track
03-27 16:15:03.877: ERROR/MediaPlayer(1057): error (-19, 0)

enter code here
android
audio
android-ndk
media-player

4 Answers

6

You need to release() the media players otherwise the resources are not released , and you soon get out of memory (since you allocate them again next time). so,I think you can play twice or even thrice... but not many times without releasing the resources

It is recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.See this link

answered on Stack Overflow Mar 27, 2012 by 5hssba • edited Mar 27, 2012 by 5hssba
2

MediaPlayer is not designed to function the way you are trying to use it. That is why you are facing the problem.

MediaPlayer is `not a good option when you are playing small sound effects as the user can click on multiple buttons very soon and you will have to create a MP object for all of them which doesn't happen synchronously and as soon as you click buttons. Go for the SoundPool Class which allows you to keep smaller sounds loaded in memory and you can play them any time you want without any lag which you would feel in a mediaplayer. http://developer.android.com/reference/android/media/SoundPool.html Here is a nice tutorial : http://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html

answered on Stack Overflow Mar 27, 2012 by Akhil
1

I think the problem is that you didint wait till the first one to finish (or stop it) to start the second .. try to listen to the previous one when it completes then launch the next one .. you may use setOnCompletionListener.

similar issue : android playing two songs problem

code:

    package ooo.iii;

    import java.io.IOException;


    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Test_audioActivity extends Activity { 
        /** Called when the activity is first created. */
        Button btn;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //        setContentView(R.layout.main); 
            setContentView(R.layout.main);
            btn=(Button)findViewById(R.id.ss);

            btn.setOnClickListener(new OnClickListener() { 

                @Override
                public void onClick(View v) {
                    playSound(R.raw.musi);

                }
            });

        }



    public void playSound(int resources){
        try{
        boolean mStartPlaying = true;
        MediaPlayer  mPlayer=null;
    if (mStartPlaying==true){

          mPlayer = new MediaPlayer();
        Uri uri = Uri.parse("android.resource://ooo.iii/" + resources);

            mPlayer.setDataSource(getApplicationContext(),uri);
            mPlayer.prepare();
            mPlayer.start();

    } 
    else{
       //   stopPlaying();
        //rePlay.setText("Replay");
        mPlayer.release();
        mPlayer = null;
    }
    mStartPlaying = !mStartPlaying;


    } 
    catch (IOException e){
        Log.e("ERR", "prepare() failed");
    }
    }



}

You need to change :

1- source @ res/raw/musi .. to yours

2- id of button at res/layout/main.xml from ss to yours ..

3- Uri uri = Uri.parse("android.resource://ooo.iii/" + resources); fix package name to yours

thats all .. I tested on real device and click many times and get new sound above previous that is still playing

answered on Stack Overflow Mar 27, 2012 by Maher Abuthraa • edited May 23, 2017 by Community
1

I had the same problem and I solved it by use of release() method in OnCompletionListener:

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), resource);
        mp.start();
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
answered on Stack Overflow Apr 23, 2017 by Piotr Sagalara

User contributions licensed under CC BY-SA 3.0