Android phone doesn't play sound with phone cordova plugin and doesn't give error message

I use an ionic structure to create an application. I would like to play the sound, so I used https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-media/ , This is my piece of code used to play the sound:

var my_media = new Media('/android/assets/www/audio/National Anthem.mp3',
        // success callback
        function () { console.log("playAudio():Audio Success"); },
        // error callback
        function (err) {console.dir("playAudio():Audio Error: " + err); }
    );
     my_media.play(); 

Unfortunately, it does not play, and the error message:

playAudio():Audio Error: [object Object]

The fact that I can not view the object on the chrome console!

If I replaced var my_media = new Media(..with var my_media =$cordovaMedia.newMedia(..., I do not receive any message on the console at all. My code is put in $ionicPlatform.ready(function().., so I know for sure that the plugin is ready before using it. Any sugesstions?

+4
2

, :

:

.factory('MediaSrv', function($q, $ionicPlatform, $window){
  var service = {
    loadMedia: loadMedia,
    getStatusMessage: getStatusMessage,
    getErrorMessage: getErrorMessage
  };

  function loadMedia(src, onError, onStatus, onStop){
    var defer = $q.defer();
    $ionicPlatform.ready(function(){
      var mediaSuccess = function(){
        if(onStop){onStop();}
      };
      var mediaError = function(err){
        _logError(src, err);
        if(onError){onError(err);}
      };
      var mediaStatus = function(status){
        if(onStatus){onStatus(status);}
      };

      if($ionicPlatform.is('android')){src = '/android_asset/www/' + src;}
      defer.resolve(new $window.Media(src, mediaSuccess, mediaError,    mediaStatus));
    });
    return defer.promise;
  }

  function _logError(src, err){
    console.error('media error', {
      code: err.code,
      message: getErrorMessage(err.code)
    });
  }

  function getStatusMessage(status){
    if(status === 0){return 'Media.MEDIA_NONE';}
    else if(status === 1){return 'Media.MEDIA_STARTING';}
    else if(status === 2){return 'Media.MEDIA_RUNNING';}
    else if(status === 3){return 'Media.MEDIA_PAUSED';}
    else if(status === 4){return 'Media.MEDIA_STOPPED';}
    else {return 'Unknown status <'+status+'>';}
  }

  function getErrorMessage(code){
    if(code === 1){return 'MediaError.MEDIA_ERR_ABORTED';}
    else if(code === 2){return 'MediaError.MEDIA_ERR_NETWORK';}
    else if(code === 3){return 'MediaError.MEDIA_ERR_DECODE';}
    else if(code === 4){return 'MediaError.MEDIA_ERR_NONE_SUPPORTED';}
    else {return 'Unknown code <'+code+'>';}
 }

  return service;
})

:

.controller('phrasebookCtrl', function ($scope, $ionicPlatform, $timeout,    $cordovaMedia,MediaSrv) { 

    $scope.play = function(src){
    MediaSrv.loadMedia(src).then(function(media){
    media.play();
});

( " " ):

 <p>Welcome: <a  ng-click="play('audio/welcome1_el.mp3')">Test me</a></p>

. Android iOS. audio www .mp3.

, , .

+3

: cdvfile src:

var my_media = new Media('cdvfile://localhost/temporary/recording.mp3', ...);

.

+1

Source: https://habr.com/ru/post/1653666/


All Articles