How to get cordova plugins to work?

Hello Im trying for hours and nothing. How to get cordovas plugin to work? In my case the “Media” plugin for example.

  1. I have this org.apache.cordova.media installed.

  2. Also i have this in all my config files:

  1. app.js

angular.module(‘starter’, [‘ionic’, ‘starter.controllers’])

.run(function($ionicPlatform){
$ionicPlatform.ready(function()
{
—> Media is undefined here.
});
})

I’m guessing not all your code is showing in this post. Please use proper code formatting.

Just to check: you’re added the plugin with cordova plugins add org.apache.cordova.media? What happens when you enter cordova plugins at the terminal?

Hello Coen, i get this:

user_eduardo@RV415SAMSUNG /c/nginx/myApp
$ cordova plugins
[ ‘org.apache.cordova.console’,
‘org.apache.cordova.device’,
‘org.apache.cordova.media’,
‘org.apache.cordova.statusbar’ ];

How i should call plugins to work inside ionic application?

angular.module(‘starter’, [‘ionic’, ‘starter.controllers’])

.run(function($ionicPlatform){
$ionicPlatform.ready(function($window)
{

Should call me plugin here? How please? 

});
})

.config(function($stateProvider, $urlRouterProvider) {
$stateProvider

.state('app', {
  url: "/app",
  abstract: true,
  templateUrl: "templates/menu.html",
  controller: 'AppCtrl'
})

.state('app.search', {
  url: "/search",
  views: {
    'menuContent' :{
      templateUrl: "templates/search.html"
    }
  }
})

.state('app.browse', {
  url: "/browse",
  views: {
    'menuContent' :{
      templateUrl: "templates/browse.html"
    }
  }
})
.state('app.playlists', {
  url: "/playlists",
  views: {
    'menuContent' :{
      templateUrl: "templates/playlists.html",
      controller: 'PlaylistsCtrl'
    }
  }
})

.state('app.single', {
  url: "/playlists/:playlistId",
  views: {
    'menuContent' :{
      templateUrl: "templates/playlist.html",
      controller: 'PlaylistCtrl'
    }
  }
});

// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise(’/app/playlists’);
});

How you call these plugins is dependent on the plugin themselves.

Check out the documentation for all plugins to see which methods are available and how you should call them.

For instance, if you want to hide the statusbar once you load the app:

$ionicPlatform.ready(function() {
   StatusBar.hide();
})

You mention you want to use the Media plugin. A quick look at the documentation for the Cordova Media plugin shows that you have quite a list of methods available:

  • media.getCurrentPosition: Returns the current position within an
    audio file.

  • media.getDuration: Returns the duration of an audio file.

  • media.play: Start or resume playing an audio file.

  • media.pause: Pause playback of an audio file.

  • media.release: Releases the underlying operating system’s audio
    resources.

  • media.seekTo: Moves the position within the audio file.

  • media.setVolume: Set the volume for audio playback.

  • media.startRecord: Start recording an audio file.

  • media.stopRecord: Stop recording an audio file.

  • media.stop: Stop playing an audio file.

So technically, you could have a controller like this:

.controller('HomeCtrl', function($scope, $stateParams) {
    
   $scope.StartRecording  = function() {
       media.startRecord();
   }

   $scope.StopRecording  = function() {
       media.stopRecord();
   }
    
})

Then in your partial have something like:

<ion-view title="Record A Sound">

  <ion-content class="has-header">

     <h1>Audio recorder</h1>
     <button ng-click="StartRecording()">Record</button>
     <button ng-click="StopRecording()">Stop recording</button>

  </ion-content>

</ion-view>

Please note that A) I have no idea what you’re trying to achieve and B) this is all untested, just from the top of my head.

My problem that i was testing it on desktop browser which cordova is not available. To work you need to test on emulator or device.

Yes, obviously. Cordova plugins only work on device since they are plugins that expose mobile device functionality through javascript to a webview. Since your browser doesn’t have this functionality, Cordova plugins will only work on device or simulator.

Is it simple to call media. to use plugin? Is there other setup to do or init into $ionicPlatform.ready?

Thx