arjun
arjun

Reputation: 1614

Calling cordova module?

I found this cordova module, there are no usage instructions though?

Writing

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

return Could not load main module: ReferenceError: MusicPlayer is not defined

The module is exported like module.exports = new MusicPlayer();, so the way to call it is that itself?

Trying, window.cordova.plugins.Musicplay.init(); also gave the same error.

What is the correct way to call this module and use in cordova?

EDIT - This is how the functions are defined

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

Upvotes: 1

Views: 197

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

Check clobbers target: 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>

So instance name should be musicplayer

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

Tips:

  • Be sure MusicPlayer plugin exists in list when you run $ cordova plugin list
  • Be sure you call musicplayer.init inside:

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

Upvotes: 1

Related Questions