ioscordovaionic-frameworkcordova-media-plugin

cordova-plugin-media-with-compression error on IOS when recording


I am using cordova-plugin-media-with-compression for an app which runs on IOS and Android. On Android the code is working perfectly, records, play, stop, erase, no problem. On IOS I can play the files saved using the android app, but as soon as I try to record I get error 1 which is MEDIA_ERR_ABORTED (no idea what that means). So works perfectly on Android, but will not record on IOS. I am also using cordovafile for other things and they work, so I know cordova file is working and I verified that I am getting a legitimate folder for the mediapath.

I used the code right out of the example.

$scope.mediapath = cordova.file.externalApplicationStorageDirectory || cordova.file.tempDirectory || cordova.file.sharedDirectory;

 $rootScope.mediaOptions = {
        SampleRate: 16000,
        NumberOfChannels: 1
    };

$scope.mediafile = "record-audio.m4a";
    $scope.media = new Media($scope.mediapath + $scope.mediafile, function () {
        console.log("recordAudio():Audio Success");
    },
        function (err) { console.log("recordAudio():Audio Error: " + err.code); },
        function (s) {
            if (s === 4) {
                $timeout(function () {
                    angular.element(document.getElementById('stopButton')).triggerHandler('click');
                }, 0);
            }
        } );
    $scope.media.startRecordWithCompression($rootScope.mediaOptions);

Solution

  • Even though . $scope.mediapath = $scope.mediapath.fullPath; worked for a while, I had further errors and ended up with this solution. Note I keep both mediapath and mediapath1 because when using $cordovaFile.readAsDataURL($rootScope.mediapath, $scope.mediafile)... I need the mediapath for ios and android.

        $rootScope.mediaOptions = {
            SampleRate: 16000,
            NumberOfChannels: 1
        };
            $rootScope.mediapath = cordova.file.tempDirectory || cordova.file.dataDirectory || cordova.file.externalApplicationStorageDirectory ||  cordova.file.sharedDirectory || cordova.file.applicationStorageDirectory;
            $rootScope.mediapath1 = $rootScope.mediapath;
            if (cordova.platformId === 'ios') {
                $rootScope.mediapath1 = '';
            }
    
    then when I need to initialize the media 
            $scope.mediafile = "audio.m4a";
    
            $scope.media = new Media($rootScope.mediapath1 + $scope.mediafile, 
              function () {
                console.log("recordAudio():Audio Success");
    
            },
              function (err) { console.log("recordAudio():Audio Error: " + err.code + err.message); },
    function (s) {
    // catch change to audio here for example when s===4 the recording has been stopped.
                   }
                });
            $scope.media.startRecordWithCompression($rootScope.mediaOptions)
    

    ;