简体   繁体   English

phoneGap中的音频不会停止

[英]Audio in phoneGap will not stop

I'm trying to make the play/stop the same element. 我正在尝试使播放/停止播放相同的元素。 The audio file plays perfectly but will not stop on second "click." 音频文件播放正常,但不会在第二次“单击”时停止。

var audio = null;
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
    audio = new Media("/android_asset/www/song.mp3", 

            if (audio) {
                audio.stop();
            }

    );
}

$(document).ready(function(){
    $('#clickhere').click(function(event){ 
        audio.play();
    });
});

Your existing stop only fires when the page is first loaded. 您的现有站点仅在首次加载页面时触发。 It won't fire again when you press 'clickhere'. 当您按“单击此处”时,它不会再次触发。 So, you need to add a toggle to the clickhere part to toggle playing on and off. 因此,您需要向clickhere部分添加切换,以切换播放的开和关。 Something like this: 像这样:

var playing = false;

$('#clickhere').click(function(event)
{
    if (playing)
    {
        audio.stop();
        playing = false;
    }
    else
    {
        audio.play();
        playing = true;
    }
});

I think you are looking to do this: 我认为您正在寻找这样做:

  if (audio.isPlaying()) {
            audio.stop();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM