Mediaplayer error (-19 0) doesn't work with release()

こ雲淡風輕ζ 提交于 2019-12-12 03:55:47

问题


I'm coding a Soundboard and I got a problem when you have clicked 20+ times on the sounds.

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

Here is a button that plays sound:

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

I have been trying with release as well then there's 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?


回答1:


Use 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 u press button and u want to play sound file ,this code will be very useful for that.




回答2:


When you used release function, MediaPlayer cleans everything on memory.

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked 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 instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.



来源:https://stackoverflow.com/questions/16172496/mediaplayer-error-19-0-doesnt-work-with-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!