1

I was installed cordova-plugin-media in ionic v1 . But media is not define by the app when I running it in the browser .

ionic.bundle.js:26794 ReferenceError: Media is not defined
at ChildScope.$scope.playPodcast (controllers.js:1405)
at fn (eval at compile (ionic.bundle.js:27638), <anonymous>:4:232)
at ionic.bundle.js:65427
at ChildScope.$eval (ionic.bundle.js:30395)
at ChildScope.$apply (ionic.bundle.js:30495)
at HTMLElement.<anonymous> (ionic.bundle.js:65426)
at defaultHandlerWrapper (ionic.bundle.js:16787)
at HTMLElement.eventHandler (ionic.bundle.js:16775)
at triggerMouseEvent (ionic.bundle.js:2953)
at tapClick (ionic.bundle.js:2942)

and this is my code

$scope.playPodcast = function($audioId) {
    new Media("http://www.viaviweb.in/envato/cc/online_mp3_app_demo/uploads/40655_Overboard.mp3").play();
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95

2 Answers2

0

Just a quick search to that plugin's github page shows the way to use that method.

Specifically if you want to play a media file, this is the correct way to use it.

function playAudio(url) {
    // Play the audio file at url
    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();

    // Pause after 10 seconds
    setTimeout(function () {
        my_media.pause();
    }, 10000);
}
0

Ok, depending on how you have the script file injected, you need to call it one of two ways:

  1. If the media plugin is being loaded as a script file that is in your index.html by itself and it attaches itself to the global namespace, then add the following to your app.js:
angular.module('Media', [])
  .factory('Media', function () {
    return window.Media;
});

Later, in your app.js module definition, add the following:

angular.module('myApp',
 [
  'ionic',
  'Media'
  ])
.run(
 [
   'ionicReady',
   'Media',
  function (
  ionicReady,  
  Media, 
) {
  //Media initialization code here
 }

This allows angular to use its Dependency Injection to ensure "Media" gets initialized in your main module. You'll have to import it to your other modules if you want to use it somewhere else in your application.

  1. If you're using NgCordova, which provides angular wrappers for common Cordova plugins, then you'd import it the same way you'd import any other AngularJS library. There's sample code for the media plugin here.
George Stocker
  • 57,289
  • 29
  • 176
  • 237