Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

playing audio sounds in Cordova application

I'm using Cordova media plugin for playing audio sound in my mobile application I tried many codes but I didn't figure out what I'm doing wrong at the bottom I put two piece of code that I tried them

the first code (js code in a separate file)

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


        var myMedia = new Media("../sounds/clapping.mp3");
        myMedia.play();

    }
};

app.initialize();

the second code (js code in a script tag) :

     document.addEventListener("deviceready", function(){

   var myMedia = null;
    function playAudio() {

    var src = "sounds/clapping.mp3";

    if(myMedia === null) {
    myMedia = new Media(src, onSuccess, onError);

    function onSuccess() {
    console.log("playAudio Success");
    }

    function onError(error) {
    console.log("playAudio Error: " + error.code);
    }

    }

    myMedia.play();
    }

    document.getElementById("playAudio").addEventListener("click", playAudio);
    });

with a button :

<button id ="playAudio">PLAY</button>

How can I solve this problem ?

like image 900
Taha Avatar asked Jul 24 '16 22:07

Taha


People also ask

How does Cordova application work?

Cordova acts as a container for the app that you write using web technologies. When the app is compiled, your code actually stays intact. The compiler just takes your code and makes it available to the web view for rendering. If you've ever opened an HTML file in a browser, that's basically the same thing.

What is media plugin?

The media plugin adds the ability for users to be able to add HTML5 video and audio elements to the editable area. It also adds the item Insert/edit video under the Insert menu as well as a toolbar button.

What are Cordova plugins?

What is a Cordova plugin? A plugin is a bit of add-on code that provides JavaScript interface to native components. They allow your app to use native device capabilities beyond what is available to pure web apps.


2 Answers

Wasted 2 hours on this, sharing it here:

This should not be this difficult. No full example at: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-media/

Simple step by step details:

  1. Put my file in www:

Example at: www/audio/button-1.mp3

  1. Install plugin:

    cordova plugin add cordova-plugin-media

  2. Copy paste code below:

`

  function getFullMediaURL(s) {
    return cordova.file.applicationDirectory + 'www/audio/button-1.mp3'
  }

  function playMP3() {
    let src = getFullMediaURL();
    var myMedia =
      new Media(src,
        function () { },
        function (e) { alert('Media Error: ' + JSON.stringify(e)); }
      );
    myMedia.play();
    myMedia.setVolume('1.0');
  }

`

Step 4: Call below where you need play sound:

playMP3();
like image 168
Manohar Reddy Poreddy Avatar answered Sep 28 '22 03:09

Manohar Reddy Poreddy


To answer your question, you can find the working sample of cordova app using Media Plugin in the following github page.

As mentioned in the sample project's README, you gotta install cordova device plugin as well to check the device platform.

Also to clarify the doubt you mentioned in the comment, android_asset refers to the project's root folder.

like image 33
Gandhi Avatar answered Sep 28 '22 04:09

Gandhi