Play sound in Meteor-Cordova app

Using a meteor without a cord, I can play sound in my browser using

new Audio('test.mp3').play() 

where test.mp3 is located in the shared folder. However, I cannot play the sound as soon as I run the application as a cordova application on the device. What am I doing wrong? Thank you

+7
cordova meteor
source share
5 answers

TL; dg

  • Install the media cord meteor add cordova: org.apache.cordova.media@0.2.15

  • Add audio files to the shared directory

  • Use /android_asset/www/application/path/to/sound.wav

If you look into this folder in your project,

$PROJECT_ROOT/.meteor/local/cordova-build/platforms/android/assets/www/

You will see your application as a cordova built for android.

Using the Cordova Media plugin, I tried to use the path to the /android_asset/www/path/to/sound.wav file /android_asset/www/path/to/sound.wav no avail.

 # Install the cordova media plugin with this command meteor add cordova: org.apache.cordova.media@0.2.15 

Instead, I saw that my sounds folder was inside the www directory, but in the application directory. So this file path has ended for me,

/android_asset/www/application/path/to/sound.wav

Here is the relevant code that worked for me.

 function getMediaUrl(sound) { if (device.platform.toLowerCase() === "android") { return cordova.file.applicationDirectory.replace('file://', '') + 'www/application/' + sound.substr(1); } else { return cordova.file.applicationDirectory.replace('file://', '') + sound.substr(1); } } function playSound(sound) { return new Media( getMediaUrl(sound), function (success) { // success }, function (err) { // error } ); } var test = playSound('/sounds/test.wav'); test.play(); 

NOTE I have included my audio files in the ie shared folder

$PROJECT_ROOT/public/sounds/test.wav

and on Android, this file path is translated to

/android_asset/www/application/sounds/test.wav

+6
source share

There seem to be two possible solutions. The first, which is probably the more correct solution, is applicable only for cordova applications. It will not work in the browser due to the dependency of the plugin. The first step is to add the cordova plugin:

  meteor add cordova: org.apache.cordova.media@0.2.15 

Then create a function to play the sound:

 playLimitSound = function() { if (Meteor.isCordova) { var my_media = new Media('http://MY_IP:port/test.mp3', // success callback function () { console.log("playAudio():Audio Success"); }, // error callback function (err) { console.log("playAudio():Audio Error: " + err); } ); // Play audio my_media.play(); } 

};

This is another solution. It works both in the browser and in the cordova application.

  new Audio('http://MY_IP:port/test.mp3').play() 
+2
source share

I had a similar issue with iOS sound notification. It was resolved when I add sound files to the Resources folder in Xcode before compiling the Xcode application. In this case, you can refer to it as:

 if (Meteor.isCordova) { var NotificationHandler = function(evt) { if (evt.sound) { var snd = new Media( Meteor.absoluteUrl(evt.sound), function() { console.log('media ok'); }, function(err) { console.log('media error',err); } ); snd.play(); } }; } 

where evt.sound is, for example, "test.mp3"

and I installed the multimedia plugin as

  meteor add cordova: org.apache.cordova.media@0.2.14 
+1
source share

If you really want to play the sound locally (without an Internet connection), you need to change the host address to:

 http://meteor.local/[your_subfolder(s)_if_you_got]/your.mp3 

Using this URL, you can, of course, use the mentioned Media or Audio calls. If your assets are located in a shared folder or resource folder, you can always read this URL in accordance with the project, these files are copied to your application package.

+1
source share

I just could not get any of the proposed solutions for working on Android, in any way or form. My Android app just silently failed while trying to play sound.

In the end, this is what worked for me:

  var player = new window.Audio(); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { player.src = window.URL.createObjectURL(this.response); player.play(); } }; xhr.open('GET', "/audio.mp3"); xhr.responseType = 'blob'; xhr.send(); 

From here .

+1
source share

All Articles