Media player error (-19 0) not working with release ()

I am coding a sound card and I got a problem where you clicked 20 times on sounds.

At first I get MediaPlayer error (-19 0) and you can't hear any more sounds. I know this is my memory. So when I try to add Release (); it doesn't sound at all.

Here is a button that plays a sound:

public void click7(View v) {
MediaPlayer mp = MediaPlayer.create(NewActivity3.this, R.raw.hahah);
mp.start(); 
    }

      

I also try with release and then no sound:

public void click7(View v) {
        MediaPlayer mp = MediaPlayer.create(NewActivity3.this, R.raw.hahah);
        mp.start();
        mp.stop();
        mp.reset();
        mp.release();
    }

      

What am I doing wrong?

0


source to share


2 answers


Use the following code

MediaPlayer mp=MediaPlayer.create(NewActivity3.this, R.raw.hahah);

   public void click7(View v) {
    if(mp!=null ){
    mp.reset();
    mp.prepare();
    mp.start(); 
   }

      



Whenever you press a button and you want to play a sound file, this code will be very helpful for that.

+1


source


When you used the release function, MediaPlayer clears everything in memory.



Releases the resources associated with this MediaPlayer. It is considered good practice to call this method when you are done with MediaPlayer... In particular, whenever an application's activity is suspended (the onPause () method is called) or stopped (the onStop () method is called), this method should be called to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and codec instances), failure to call this method immediately when the MediaPlayer object is no longer needed can also result in continuous battery drain for mobile devices and playback failure for other applications if not supported on the device. multiple instances of the same codec. Even if multiple instances of the same codec are supported, you can expect some performance degradation when using unnecessary multiple instances at the same time.

0


source







All Articles