调用cordova模块?

时间:2017-10-21 15:29:02

标签: javascript cordova

我找到this cordova module,虽然没有使用说明?

写作

MusicPlayer.init(
        function (msg) {
            console.log("audio completed"+ msg);
        },
        // error callback
        function (e) {
            console.log("Error getting message=" + e);
        }
    );

返回Could not load main module: ReferenceError: MusicPlayer is not defined

模块的导出方式与module.exports = new MusicPlayer();类似,所以调用它的方式就是它自己吗?

尝试,window.cordova.plugins.Musicplay.init();也犯了同样的错误。

调用此模块并在cordova中使用的正确方法是什么?

编辑 - 这是函数的定义方式

var exec = require('cordova/exec');

function MusicPlayer() {}

MusicPlayer.prototype.init = function(successCallback, errorCallback, json) {
    exec(successCallback, errorCallback, "MusicPlayer", "init", [json]);
};    
MusicPlayer.prototype.getMusicList = function(successCallback, errorCallback, json) {
    exec(successCallback, errorCallback, "MusicPlayer", "getMusicList", [json]);
};
..
..
module.exports = new MusicPlayer();

1 个答案:

答案 0 :(得分:1)

检查clobbers目标:https://github.com/jasminpethani/cordova-plugin-musicplayer/blob/master/com.srini.musicplayer/plugin.xml#L14-L16

<js-module src="www/musicplayer.js" name="musicplayer">
    <clobbers target="musicplayer" />
</js-module>

因此实例名称应为musicplayer

   musicplayer.init(function (msg) {
        console.log("audio completed"+ msg);
    },
    // error callback
    function (e) {
        console.log("Error getting message=" + e);
    });

提示:

  • 运行MusicPlayer
  • 时,请确保列表中存在$ cordova plugin list插件
  • 请务必在内部致电musicplayer.init

    document.addEventListener("deviceready", function(){/**/}