我正在使用cordova(媒体插件)和visual studio 2015开发一个简单的Android应用程序,它只播放音乐,我想在触发某个事件后暂停音乐。
在下面的代码形式index.js中,我试图在10秒后暂停音乐。但它不起作用,
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints,
// and then run "window.location.reload()" in the JavaScript Console.
var my_media = null;
(function () {
"use strict";
document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );
function onDeviceReady() {
document.getElementById("playmusic").addEventListener("click", onClickPlayMusic, false);
The musci is now getting played. Now I want to pause() the music
setTimeout(function () {
alert('Going to Pause');
if (my_media) {
my_media.pause(); // this pasue() does not work. The musci contine to play.
}
}, 10000);
};
function onClickPlayMusic() {
if (document.getElementById("playmusic").innerHTML == "Play" & my_media == null) {
console.log("Playing the music");
var src = "file:///android_asset/www/music.mp3";
var my_media = new Media(src);
my_media.play();
}
}
})();
如果我在onClickPlayMusic()函数中添加暂停代码,它的效果非常好。
请让我知道如何摆脱这个问题。
TIA
答案 0 :(得分:0)
解决了问题:
而不是
var my_media = new Media(src);
我做了
my_media = new Media(src);
因此可以从整个页面访问变量。