我正在寻找Cordova插件 - 媒体工作示例(演示)

时间:2015-10-08 18:03:24

标签: android cordova

我无法在我的cordova应用中添加声音。我找不到关于cordova-plugin-media的npm / github文档的例子:
https://www.npmjs.com/package/cordova-plugin-media
https://github.com/apache/cordova-plugin-media
我也试图实现这两个:
Play sound on Phonegap app for Android
audio not working with phoneGap

到目前为止没有运气。我在哪里可以找到最新的复制和粘贴示例?非常感谢!

(Cordova版本5.1.1,我通过cordova plugin add cordova-plugin-media安装插件)

1 个答案:

答案 0 :(得分:2)

您的index.js文件应该如下所示,并在index.html中删除自己编写的javascript:

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        app.receivedEvent('deviceready');

        var myMedia = new Media("http://www.noiseaddicts.com/samples_1w72b820/3926.mp3")
        myMedia.play({ playAudioWhenScreenIsLocked : false })

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