cordova-plugin-media最新版本我做错了什么

时间:2017-05-17 06:58:02

标签: android cordova plugins media

我无法让cordova-plugin-media正常运行。我正在使用Android工作室和Mac的Android模拟器,这是我的代码。我在index.html文件中完成了一次onclick,但没有任何反应。我在index.js文件中添加了媒体插件的代码。 所有codrova文件和媒体插件都已安装并添加。

index.js

var app = {
// Application Constructor
initialize: function() {
    document.addEventListener('deviceready', 
this.onDeviceReady.bind(this), false);
},

// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
    this.receivedEvent('deviceready');

var my_media = null;
// Play audio
//
function playAudio(src) {
// Play the audio file at url
var my_media = new Media(src,
    // success callback
    function () {
        console.log("playAudio():Audio Success");
    },
    // error callback
    function (err) {
        console.log("playAudio():Audio Error: " + err);
    }
);
// Play audio
my_media.play();
} 


},

// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};

app.initialize();

index.html

<div data-role="main" class="ui-content">
            <span class="ui-block-a">
            <img src="img/grongoling.jpg"><h2>Gröngöling</h2>
            <a href="#" class="ui-btn ui-corner-all ui-icon-audio ui-
btn-icon-notext right" onclick="playAudio('sounds/grongoling.mp3');">
</a>
            </span>
            <span class="ui-block-a">
            <img src="img/kattuggla.jpg"><h2>Kattuggla</h2>
            <a href="#" class="ui-btn ui-corner-all ui-icon-audio ui-
btn-icon-notext right" 
onclick="playAudio('sounds/kattuggla.mp3');"></a>
            </span>
            <span class="ui-block-a">
            <img src="img/korp.jpg"><h2>Korp</h2>
            <a href="#" class="ui-btn ui-corner-all ui-icon-audio ui-
btn-icon-notext right" 
onclick="playAudio('sounds/korp.mp3');"></a>
            </span>
            <span class="ui-block-a">
            <img src="img/storrehackspett.jpg"><h2>Större 
Hackspett</h2>
            <a href="#" class="ui-btn ui-corner-all ui-icon-audio ui-
btn-icon-notext right" 
onclick="playAudio('sounds/storrehackspett.mp3');"></a>
            </span>
            <span class="ui-block-a">
            <img src="img/tofsvipa.jpg"><h2>Tofsvipa</h2>
            <a href="#" class="ui-btn ui-corner-all ui-icon-audio ui-
btn-icon-notext right" 
onclick="playAudio('sounds/tofsvipa.mp3');"></a> 
            </span>
            </div>

1 个答案:

答案 0 :(得分:0)

我认为您必须在onDeviceReady()函数之外定义playAudio()函数,以使其可以全局访问,并且能够直接从HTML调用它。

此外,您在函数外部初始化my_media变量,并使用函数内部的var再次重新创建它。考虑创建一个全局变量my_media并在没有var的函数中设置它,或者每次在函数中创建它。