Cordova media plugin

Does it still work on latest ionic ? I’ve tried example from cordova and some examples from forum but nothing’s work. can some one show me a working example ?

got and error on console Error: Media is not defined

Can you share some code on what you’ve tried? It will help to troubleshoot.

Thanks,

I’ve used it with great success on my latest project using Ionic Beta 14.

As Nic mentioned - if you could share some code samples, it will help us to assist you.

I’ve followed the example from cordova.

angular.module('starter.controllers', ['ionic', 'ngCordova'])
.controller('DashCtrl', function($scope, $cordovaMedia) {
	$scope.play = function() {
		var src = "sound/speech.mp3";
		var media = $cordovaMedia.newMedia(src).then(function() {
			media.play();
		}, function () {
			// error
		});
      }
})

The audio is in the www/sound folder. This is what i actually want to play

 http://translate.google.com/translate_tts?tl=en&q=this%20is%20an%20example%20speech

Then i called the function from html page

<button class="button" ng-click="play()">Play</button>

I’ve included cordova.js on index.html

<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>

I’ve tried it on device and browser, here’s the error log

"Error: Media is not defined
.newMedia@file:///D:/nihon/www/js/ng-cordova.min.js:8:12585
$scope.play@file:///D:/nihon/www/js/controllers.js:6:15
$parseFunctionCall@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:20124:15
@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:50863:9
$RootScopeProvider/this.$get</Scope.prototype.$eval@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:22178:16
$RootScopeProvider/this.$get</Scope.prototype.$apply@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:22276:18
@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:50862:7
createEventHandler/eventHandler@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:10823:9
triggerMouseEvent@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:2811:3
tapClick@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:2800:3
tapMouseUp@file:///D:/nihon/www/lib/ionic/js/ionic.bundle.js:2870:5
" ionic.bundle.js:19387

above is my code, i’m using latest ionic version.

Well, at first glance, it all looks OK. Just a routine question, but have you actually installed the media plugin as well? i.e. by running:

ionic plugin add org.apache.cordova.media

on the command line? Sounds like the ‘Media’ class cannot be found?? Also try removing and re-adding your platform, i.e.

ionic platform remove ios
ionic platform add ios

to see if that makes a difference.

Yes i’ve installed the plugin using ionic and cordova. I’ve tried remove and re-add the platform but still same error. i even tried to make clean project and tried on both device and browser.

One more thing to try, and I think I had to do this on one of my projects…

Replace the line:

var media = $cordovaMedia.newMedia(src).then(function() {...

with

var media = new Media(src);
var mediaPromise = media.promise.then(function() {...

and see how that goes…

Still getting same error. I’m using html5 audio now, it works better, but only in browser tho

Yes, I ended up using HTML5 audio on one of my projects too. It works fine in iOS too. Android needs the Crosswalk browser extension though…

That leads me to believe that you are trying to test this from your web browser. You cannot test native device plugins such as Media, from your web browser.

You must use a device or simulator.

Regards,

I’m using this plugin, but I get this error.

Error: [$injector:unpr] Unknown provider: $cordovaMediaProvider <- $cordovaMedia <- myCtrl

I add the plugin via CLI ionic

 ionic plugin add org.apache.cordova.media

This is my controller

.controller('myCtrl',function($scope,$stateParams,LuoghiService, $cordovaMedia) {
        $scope.play = function () {
            var src = "audio/music.mp3";
            var media = $cordovaMedia.newMedia(src).then(function() {
                media.play();
            }, function () {
                // error
            });
       }
 })

Can someone help me?

this plugin only works if you try it on device

1 Like

I’m facing the same issue.
Error: [$injector:unpr] Unknown provider: $cordovaMediaProvider <- $cordovaMedia
Does someone has a solution?

Check the version of ng-cordova that you are using. It looks like as of 0.1.18-alpha that they started using “$cordovaMedia2” instead of $cordovaMedia. I was hitting the same problem and switching to $cordovaMedia2 fixed it.

2 Likes

Thanks! this worked for me :smile:

Was this emulated in the browser or the ionic Lab?

Hi nicraboy,

I am following your tutorial to play a record in my list but I am getting the same error here.

122020   error    ReferenceError: Media is not defined
at new i (http://192.168.0.101:8100/js/ng-cordova.min.js:9:4795)