Play Sound In Meteor-Cordova App

ⅰ亾dé卋堺 提交于 2019-12-19 06:39:53

问题


Using meteor without cordova I can play a sound fine in my browser using

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

where test.mp3 is located in the public folder. However I cannot play the sound once I run my app as a cordova app on a device. What am I doing wrong? Thanks.


回答1:


tl;dr

  1. Install cordova media plugin meteor add cordova:org.apache.cordova.media@0.2.15

  2. Add audio files to public directory

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

If you look inside this folder in your project,

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

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

Using the Cordova Media plugin, I attempted to use the file path /android_asset/www/path/to/sound.wav to 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 under an application directory. So, this file path ended up working for me,

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

Here's 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 included my audio files in the public folder i.e.

$PROJECT_ROOT/public/sounds/test.wav

and on Android, this file path is translated to

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




回答2:


There seems to be two possible solutions. The first which is probably the more correct solution is only valid for cordova apps. It will not work in the browser due to the plugin dependency. First step is to add the cordova plugin:

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

Next build 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 browser and cordova-app.

 new Audio('http://MY_IP:port/test.mp3').play()



回答3:


I had a similar problem with iOS push notification sound. It was solved when I add sound files to Resources folder in Xcode before Xcode compile the app. 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 had media plugin installed as

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



回答4:


If you really want to play the sound locally (no internet connection) then you need to change your host address to:

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

With this URL you can of course do the mentioned Media or Audio calls. If your assets are in public folder or the resources folder then you can always assume this URL as per design these files are copied into your app bundle.




回答5:


I just couldn't get any of the proposed solutions to work on Android, in any way shape or form. My app, on Android, would just silently fail when trying to play sound.

In the end, this is what did work 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.



来源:https://stackoverflow.com/questions/27432554/play-sound-in-meteor-cordova-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!