No promise returned with $cordovaMedia.newMedia

Hi,

I am trying to use the ngCordova to play a sound. Here is the code:

function MusicCtrl($cordovaMedia) {
    var vm = this;
    
    vm.play = function() {
        var media = $cordovaMedia.newMedia("htc_windows_8.mp3").then(function() {
            console.log("Success");
    
        }, function(err) {
          console.log("Failed: " + err);
    });

    media.play();

};

For some reason, I don’t have “Success” or “Failed”. Why? (seems somebody has the same issue here ngCordova Media plugin not working)

But from my side, I have something in the console:

Error: 'undefined' is not a function (evaluating 'media.play()')
play@local:///js/modules/musicCtrl.js:27:16
$parseFunctionCall@local:///lib/ionic/js/ionic.bundle.js:20124:22
local:///lib/ionic/js/ionic.bundle.js:50863:20
$eval@local:///lib/ionic/js/ionic.bundle.js:22178:27
$apply@local:///lib/ionic/js/ionic.bundle.js:22276:27
local:///lib/ionic/js/ionic.bundle.js:50862:18
eventHandler@local:///lib/ionic/js/ionic.bundle.js:10823:24
dispatchEvent@[native code]
triggerMouseEvent@local:///lib/ionic/js/ionic.bundle.js:2811:19
tapClick@local:///lib/ionic/js/ionic.bundle.js:2800:19
tapTouchEnd@local:///lib/ionic/js/ionic.bundle.js:2918:12 

I am using a Blackberry Q10 for development with Webworks. The ngCordova worked for file transmission, but it seems the media one does not.

Thanks in advance for your comments!

nobody has some advices to give me to verify what is going on?

I haven’t used the Media plugin with ngCordova but in their docs it says that it is only supported on iOS, Android and WP.

I know the official docs says they support BlackBerry 10 but not sure if ngcordova does it.

Have you tried using the plugin directly without ngcordova?

Hi joseadrian,

Just retried plain cordova:

vm.play = function() {
  
  // Play the audio file at url
  var url = "htc_windows_8.mp3";
  var my_media = new Media(url,
      // success callback
      function () {
          console.log("playAudio():Audio Success");
      },
      // error callback
      function (err) {
          console.log("playAudio():Audio Error: " + err);
      }
  );
  // Play audio
  my_media.play(); 
};

Nothing on the console and got this error from the phone:

Media Error
This media file is of an unsupported type or could not be loaded. It cannot be played.

I tried another sound type, wav and got the same thing. :frowning:

I guess if you put a file that doesn’t exist you will get a message “File not found” but IF you get the same message as the one you got then MAYBE the path is not correct.

Also try with an external file from the phonegap examples => http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3. If THAT works then you probably need to fix your files.

joseadrian,

I just tried the link that you suggested me and it worked! Still not going to my success callback though, but it plays the mp3.

So now, how come it does not play local application sound?

I found this page:

https://blog.nraboy.com/2014/11/playing-audio-android-ios-ionicframework-app/

At the bottom of the article, he says:

On Android, if you’d like to play audio bundled with your application you would need to include /android_asset/ at the beginning of your path

Does this special path exists for Blackberry 10? I am a bit surprised to see that. Does it mean that you need to have different path depending of the OS you are targeting for? Not easy to be universal that way, don’t you think?

Well… I guess what the Media plugin provides is a cross platform way to reproduce audio files but just that. I don’t know. Ask the creators. Haha.

Reading the comments section, the author suggested someone to try this line to get the path of his app:

cordova.file.applicationDirectory

And concatenating it with the audio path, it should work.

myAudioFile = cordova.file.applicationDirectory + “www/path/to/the/audio/file.ext”;

If the audio plays, then just keep playing with the plugin.

I tried the applicationDirectory and it returned “undefined”!

I tried also “window.location.pathname”, did not work

Do you have the Fileplugin installed? https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

This may not be a reason but… are you testing on an emulator?

Yeah, just double check. Anyway, when adding the media plugin, it added the file one too (Blackberry Webworks tools)

How can I make sure that cordova and its plugin are correctly installed to my device?

And to answer your last question, I am testing on the device directly.