# Cordova media plugin

**URL:** <https://forum.ionicframework.com/t/cordova-media-plugin/16975>\
**Category:** uncategorized\
**Created:** [February 4, 2015, 6:18pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975 "2015-02-04T18:18:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [February 4, 2015, 6:18pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/1 "2015-02-04T18:18:14Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [February 4, 2015, 9:21pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/2 "2015-02-04T21:21:14Z")

</div>

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

Thanks,

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [February 4, 2015, 10:56pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/3 "2015-02-04T22:56:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [February 5, 2015, 4:26am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/4 "2015-02-05T04:26:50Z")

</div>

> [@nicraboy](#):
>
> Can you share some code on what you’ve tried? It will help to troubleshoot.
> 
> Thanks,

> [@CyberFerret](#):
>
> 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

```

---

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [February 5, 2015, 4:28am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/5 "2015-02-05T04:28:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [February 5, 2015, 5:33am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/6 "2015-02-05T05:33:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [February 5, 2015, 6:04am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/7 "2015-02-05T06:04:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [February 5, 2015, 6:31am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/8 "2015-02-05T06:31:37Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [February 5, 2015, 9:00am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/9 "2015-02-05T09:00:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [February 5, 2015, 11:35am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/10 "2015-02-05T11:35:07Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![nicraboy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/nicraboy/32/6917_2.png) [@nicraboy](https://forum.ionicframework.com/u/nicraboy)\
**Post date:** [February 5, 2015, 3:58pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/11 "2015-02-05T15:58:40Z")

</div>

> [@flameloki](#):
>
> file:///D:

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,

---

<div class="post-metadata">

**Author:** ![Farruggia](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Farruggia](https://forum.ionicframework.com/u/Farruggia)\
**Post date:** [June 15, 2015, 8:22pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/12 "2015-06-15T20:22:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![flameloki](https://avatars.discourse-cdn.com/v4/letter/f/8491ac/32.png) [@flameloki](https://forum.ionicframework.com/u/flameloki)\
**Post date:** [July 13, 2015, 11:42am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/13 "2015-07-13T11:42:56Z")

</div>

this plugin only works if you try it on device

---

<div class="post-metadata">

**Author:** ![Hosar](https://avatars.discourse-cdn.com/v4/letter/h/958977/32.png) [@Hosar](https://forum.ionicframework.com/u/Hosar)\
**Post date:** [August 8, 2015, 12:48am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/14 "2015-08-08T00:48:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peterholcomb](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/peterholcomb/32/90787_2.png) [@peterholcomb](https://forum.ionicframework.com/u/peterholcomb)\
**Post date:** [August 11, 2015, 11:46am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/15 "2015-08-11T11:46:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jasdeep](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jasdeep/32/90820_2.png) [@jasdeep](https://forum.ionicframework.com/u/jasdeep)\
**Post date:** [August 17, 2015, 11:25pm UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/16 "2015-08-17T23:25:43Z")

</div>

Thanks! this worked for me 😄

---

<div class="post-metadata">

**Author:** ![Shinobi81](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Shinobi81](https://forum.ionicframework.com/u/Shinobi81)\
**Post date:** [October 16, 2015, 5:25am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/17 "2015-10-16T05:25:00Z")

</div>

Was this emulated in the browser or the ionic Lab?

---

<div class="post-metadata">

**Author:** ![sam747](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@sam747](https://forum.ionicframework.com/u/sam747)\
**Post date:** [May 29, 2017, 7:21am UTC](https://forum.ionicframework.com/t/cordova-media-plugin/16975/18 "2017-05-29T07:21:24Z")

</div>

Hi nicraboy,

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

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

```
