phonegap media sound stop playing after 5 times

随声附和 提交于 2019-11-30 09:15:37

Android has a finite amount of resources in order to play sounds. You keep creating a new Media object each time you want to play a file. You need to release the sounds you are not using. If you need to play those sounds multiple times each then consider creating separate media objects to hold a reference to those sounds.

Read up on media.release.

I had the same problem. The solution I did.:

     if(media){ 
       media.stop();
       media.release();
     }
     media = new Media(...);

Here's my trick: I released it upon it finished playing or an error occurred.

function playAudio(url) {
    try {

        var my_media = new Media(url,
            // success callback
            function () {
                **my_media.release();**
            },
            // error callback
            function (err) {
                **my_media.release();**
            });

        // Play audio
        my_media.play();
    } catch (e) {
        alert(e.message);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!