Ionic: Media plugin - can't get it working

This worked in the old version of ionic , but now I have finished updating the rest of this application and returned to completion, the sound will not play.

I have an on / off switch to play demo sound in an application. Here is the code, with comments and with a comment that I thought might cause my problem. This is not true.

 .controller( 'SoundCtrl', function( $scope, $timeout ) { $scope.sound_on = false; var media = new Media( '100bpm.wav' ); $scope.soundPlayer = function() { console.log( "in soundPlayer" ); if( $scope.sound_on == false ) { $scope.sound_on = true; media.setVolume( '1.0' ); media.play(); console.log( "sound on" ); console.log( media ); /*$timeout(function(){ $scope.sound_on=false; console.log("should change"); }, 12600);*/ } else { media.stop(); $scope.sound_on = false; console.log( "sound off" ); } } }); 

I get all the necessary console logs and I put the wav file in the same folder as my js scripts.

Still nothing.

Any help?

+6
source share
1 answer

According to this post on the Ionic forum, you need to add '/ android_asset / www /' to the path of your Android media file.

So your code will look like this:

 $scope.media = new Media( '/android_asset/www/'+'100bpm.wav', function() { console.log("[mediaSuccess]"); }, function(err) { console.log("[mediaError]", err); }, function(status) { console.log("[mediaStatus]", status); }); 

In my trial version, I used the (scope) property of the $ scope controller to store the media object, and I also added successHandler, errorHandler, statusHandler

NB: note that the Media class (function) is already available when you create it using new media() in SoundCtrl. It seems to me that SoundCtrl will be created before onDeviceReady (that is, when cordova.plugin.media will be available), so I added the new Media(...) function to $scope.soundPlayer() .

+5
source

All Articles