调用playAudio()30至40次后,Phonegap声音停止播放 [英] Phonegap Sound stops playing after calling playAudio() 30 to 40 times

查看:92
本文介绍了调用playAudio()30至40次后,Phonegap声音停止播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在单击鼠标时给出了声音响应.这就是我做过的

In my app i have given sound response on mouse click. This is how i have done

我已经调用了这样的函数

i have called the function like this

<center><input id="click_flip" type="button" value="Click Me" class="clickme" onclick="callplay()"></center>

这是功能

<script type="text/javascript" charset="utf-8">

        function callplay()
        {
                                         if(voice=="male")
                                        playAudio('/android_asset/www/Mobile/sound/Male/'+rand1+'.mp3');
                                        else
                                        playAudio('/android_asset/www/Mobile/sound/Female/'+rand1+'.mp3');
        }
        // Audio player
        //

      var my_media = null;

        // Play audio
        //
        function playAudio(src) {




                // Create Media object from src

                my_media = new Media(src, onSuccess, onError);
            // else play current audio
            // Play audio
            my_media.play();



        }





        // onSuccess Callback
        //
        function onSuccess() {
            console.log("playAudio():Audio Success");
        }

        // onError Callback 
        //
        function onError(error) {
         //   alert('code: '    + error.code    + '\n' + 
                  'message: ' + error.message + '\n');
        }


        </script>

但是当我重复按钮时,单击多次(大约30到40)次.声音没有任何反应.

But when i am repeating the button click multiple (around 30 to 40 ) times. The sound is not giving any responses.

之后,使用此链接,我已经添加了此内容功能

After that using this link i have added this function

if(my_media){ 
   my_media.stop();
   my_media.release();
 }

也尝试过

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);
    }
}

但不起作用.请建议

推荐答案

我遇到了同样的问题,现在我尝试使用这种方式,并且似乎可以正常工作:

I have the same problem, now I try to use this way and it seems working:

var my_media = null;

function playAudio(src)
{
    if(my_media != null) my_media.release();
    my_media = new Media(src);
    my_media.play();
}

如果您释放硬件,则不需要停止,因为释放会自动停止android中的当前媒体.以某种方式,如果您尝试在成功或错误回调中释放,则它并不总是有效.有时候,当我尝试播放尚未完成的Media时,这会破坏Media对象.

If you release the hardware you dont need to stop, because the release automatic stop the current media in android. Somehow if you try release in the success or in the error callback it does not always work. There are times when I try to play when the Media is not finished yet and this break the Media object.

这篇关于调用playAudio()30至40次后,Phonegap声音停止播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆