phonegap media sound stop playing after 5 times

前端 未结 3 1013
甜味超标
甜味超标 2020-12-31 17:11

I need to play the \"correct\" sound if user answer the question correctly and \"wrong\" if incorrect. The sound is playing at first but after at the fifth time I answer cor

相关标签:
3条回答
  • 2020-12-31 17:57

    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.

    0 讨论(0)
  • 2020-12-31 18:08

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

         if(media){ 
           media.stop();
           media.release();
         }
         media = new Media(...);
    
    0 讨论(0)
  • 2020-12-31 18:12

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题