简体   繁体   English

PhoneGap媒体插件停止音频不工作

[英]PhoneGap media plugin stop the audio not working

I am workign with PhoneGap 3.0.0 and my confic.xml file does have the needed plugins. 我正在使用PhoneGap 3.0.0,我的confic.xml文件确实有所需的插件。 I can start saying that my sound do start when i run my code, and that everything else works. 我可以开始说我的声音在我运行我的代码时开始,而其他一切都有效。 Just 1 thing dont want to work. 只有一件事不想工作。

I am using Jquery Mobile. 我正在使用Jquery Mobile。

I cant stop the audio. 我不能停止音频。

I am calling a function called "stopAudio()" that read a global variable "my_audio" and fire of the "stop()" function. 我正在调用一个名为“stopAudio()”的函数,它读取全局变量“my_audio”并激活“stop()”函数。

Here is my code. 这是我的代码。

Index.html 的index.html

<a href="#stopaudio" data-role="button">Stop Audio</a>

AudioHandler.js AudioHandler.js

document.addEventListener("deviceready", onDeviceReady, false);

var my_media = null;

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


function stopAudio() {
    my_media.stop();
}

controller.js controller.js

$("#stopaudio").click(function() {
    stopAudio();
});

Example usage: 用法示例:

playAudio("/android_assets/www/mysound.mp3");

So i have these 3 files. 所以我有这3个文件。 What i posted is a small amount of the content, but the other content in those files have nothing to do with the audio. 我发布的是少量内容,但这些文件中的其他内容与音频无关。 I did also try and comment everything out and so forth. 我也尝试过评论所有内容等等。

So, basicly the "controller.js" is the file where the click are detected, and from there the function "stopAudio()" are called that comes from AudioHandler.js. 因此,基本上“controller.js”是检测到点击的文件,并从那里调用来自AudioHandler.js的函数“stopAudio()”。

My audio do start on startup of the phone. 我的音频会在手机启动时启动。

I cant stop the audio. 我不能停止音频。

Im running out of ideas, any help would be welcome :-) 我没有想法,欢迎任何帮助:-)

Thanks :) 谢谢 :)

You've declared my_media as a local variable here: 您已将my_media声明为局部变量:

try {
        var my_media = new Media(url,

You want it to be global, so remove the var : 您希望它是全局的,因此删除var

function playAudio(url) {
    try {

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

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

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