-1

I'm having trouble adding sound to my cordova app. I can't find an example on the npm/github documentation of cordova-plugin-media:
https://www.npmjs.com/package/cordova-plugin-media
https://github.com/apache/cordova-plugin-media
I've also tried to implement these two:
Play sound on Phonegap app for Android
audio not working with phoneGap

No luck so far. Where can I find an up-to-date copy and paste example that works? Big thanks!

(Cordova version 5.1.1, I install the plugin through cordova plugin add cordova-plugin-media)

Community
  • 1
  • 1
Baki
  • 564
  • 2
  • 10
  • 22
  • The examples from https://github.com/apache/cordova-plugin-media are working, I used them a few days ago. – Joerg Oct 08 '15 at 18:11
  • I can't find a full example, an index.html with everything in it. There are just JS examples that don't work on their own, they need to be a part of something bigger. – Baki Oct 08 '15 at 18:18
  • You have du add the code examples to your cordova javascript file. If you don't know how to write a cordova app, then please a have look at the demo cordova app, which you get after the install of cordova and read the documentation from cordova. – Joerg Oct 08 '15 at 18:56
  • I know how to write a cordova app. What exactly do I need to app? I keep running in circles, I have no idea if I'm copying the right stuff and where the problem may lie. I'm currently trying to test it out through genymotion. – Baki Oct 08 '15 at 19:11
  • Please show your app code. – Joerg Oct 08 '15 at 19:19
  • I just have a standard cordova create to which I try to add sound... https://jsbin.com/dadinagaro/edit?html – Baki Oct 08 '15 at 19:28

1 Answers1

2

Your index.js file should look like this, and remove your self written javascript in your index.html:

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();
Joerg
  • 3,102
  • 2
  • 24
  • 30