Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic: Media plugin - can’t get it working

This had been working in an old version of ionic but now I've finished updating the rest of this app and gone back to finalise this the sound won't play.

I have an on-off switch for playing a demo sound in the app. Here is the code, with comments and with the commented out part I thought might be causing my problem. It isn't.

.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 right console logs, and I put the wav file in the same folder as my js scripts.

Still nothing.

Any help?

like image 845
Subjective Effect Avatar asked Dec 30 '15 10:12

Subjective Effect


1 Answers

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

So your code becomes as below:

$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 I used a property (media) of the controller $scope to store media object and I attached also successHandler, errorHandler, statusHandler

N.B.: pay attention that Media class (function) is already available when you instantiate it with new media() in SoundCtrl. It happens to me that SoundCtrl be created before onDeviceReady (that is when cordova.plugin.media becomes available), so I added new Media(...) to $scope.soundPlayer() function.

like image 200
beaver Avatar answered Sep 19 '22 12:09

beaver