I want record audio, but have this problem “TypeError: Cannot read property ‘capture’ of undefined”, cordova-media-capture doesnt work, can anyone help me, thanks
Controller code:
recordCtrl.js
.controller(‘recordCtrl’, function ($scope, Sounds, $state, $ionicHistory) {
$scope.sound = {name: ""};
$scope.saveSound = function () {
console.log('trying to save ' + $scope.sound.name);
//Simple error checking
if ($scope.sound.name === "") {
navigator.notification.alert("Name this sound first.", null, "Error");
return;
}
if (!$scope.sound.file) {
navigator.notification.alert("Record a sound first.", null, "Error");
return;
}
/*
begin the copy to persist location
first, this path below is persistent on both ios and and
*/
var loc = cordova.file.dataDirectory;
/*
but now we have an issue with file name. so let's use the existing extension,
but a unique filename based on seconds since epoch
*/
var extension = $scope.sound.file.split(".").pop();
var filepart = Date.now();
var filename = filepart + "." + extension;
console.log("new filename is " + filename);
window.resolveLocalFileSystemURL(loc, function (d) {
window.resolveLocalFileSystemURL($scope.sound.file, function (fe) {
fe.copyTo(d, filename, function (e) {
console.log('success inc opy');
console.dir(e);
$scope.sound.file = e.nativeURL;
$scope.sound.path = e.fullPath;
Sounds.save($scope.sound).then(function () {
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("home");
});
}, function (e) {
console.log('error in coipy');
console.dir(e);
});
}, function (e) {
console.log("error in inner bullcrap");
console.dir(e);
});
},
function (e) {
console.log('error in fs');
console.dir(e);
}
);
$state.go('app.recordsList', {
id: $root.stateParams.id,
docId: $root.stateParams.docId,
fieldId: $root.stateParams.fieldId
});
};
var captureError = function (e) {
console.log('captureError', e);
};
var captureSuccess = function (e) {
console.log('captureSuccess');
console.dir(e);
$scope.sound.file = e[0].localURL;
$scope.sound.filePath = e[0].fullPath;
};
$scope.record = function () {
navigator.device.capture.captureAudio(
captureSuccess, captureError, {duration: 10});
};
$scope.play = function () {
if (!$scope.sound.file) {
navigator.notification.alert("Record a sound first.", null, "Error");
return;
}
var media = new Media($scope.sound.file, function (e) {
media.release();
}, function (err) {
console.log("media err", err);
});
media.play();
}
});
index:
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova scripts begin (this will be a 404 during development!) -->
<script src="cordova.js"></script>
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<!-- cordova scripts end (this will be a 404 during development!) -->