Search code examples
androidaudioandroid-ndkmedia-player

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


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

Solution

  • 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