Ionic: Media plugin - can’t get it working

旧城冷巷雨未停 提交于 2019-12-10 13:35:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34527664/ionic-media-plugin-can-t-get-it-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!