captureAudio: function()

in src/windows/CaptureProxy.js [299:392]


    captureAudio: function (successCallback, errorCallback, args) {
        const options = args[0];

        const audioOptions = new CaptureAudioOptions();
        if (typeof options.duration === 'undefined') {
            audioOptions.duration = 3600; // Arbitrary amount, need to change later
        } else if (options.duration > 0) {
            audioOptions.duration = options.duration;
        } else {
            errorCallback(new CaptureError(CaptureError.CAPTURE_INVALID_ARGUMENT));
            return;
        }

        // Some shortcuts for long namespaces
        const CaptureNS = Windows.Media.Capture;
        const MediaPropsNS = Windows.Media.MediaProperties;
        const localAppData = Windows.Storage.ApplicationData.current.localFolder;
        const generateUniqueName = Windows.Storage.NameCollisionOption.generateUniqueName;

        const mediaCapture = new CaptureNS.MediaCapture();
        const mediaCaptureSettings = new CaptureNS.MediaCaptureInitializationSettings();
        const mp3EncodingProfile = new MediaPropsNS.MediaEncodingProfile.createMp3(MediaPropsNS.AudioEncodingQuality.auto); // eslint-disable-line new-cap
        const m4aEncodingProfile = new MediaPropsNS.MediaEncodingProfile.createM4a(MediaPropsNS.AudioEncodingQuality.auto); // eslint-disable-line new-cap

        mediaCaptureSettings.streamingCaptureMode = CaptureNS.StreamingCaptureMode.audio;

        let capturedFile;
        let stopRecordTimeout;

        const stopRecord = function () {
            mediaCapture.stopRecordAsync().then(
                function () {
                    capturedFile.getBasicPropertiesAsync().then(
                        function (basicProperties) {
                            const result = new MediaFile(
                                capturedFile.name,
                                'ms-appdata:///local/' + capturedFile.name,
                                capturedFile.contentType,
                                basicProperties.dateModified,
                                basicProperties.size
                            );
                            result.fullPath = capturedFile.path;
                            successCallback([result]);
                        },
                        function () {
                            errorCallback(new CaptureError(CaptureError.CAPTURE_NO_MEDIA_FILES));
                        }
                    );
                },
                function () {
                    errorCallback(new CaptureError(CaptureError.CAPTURE_NO_MEDIA_FILES));
                }
            );
        };

        mediaCapture.initializeAsync(mediaCaptureSettings).done(function () {
            localAppData.createFileAsync('captureAudio.mp3', generateUniqueName).then(
                function (storageFile) {
                    capturedFile = storageFile;
                    mediaCapture.startRecordToStorageFileAsync(mp3EncodingProfile, capturedFile).then(
                        function () {
                            stopRecordTimeout = setTimeout(stopRecord, audioOptions.duration * 1000);
                        },
                        function (err) {
                            // -1072868846 is the error code for "No suitable transform was found to encode or decode the content."
                            // so we try to use another (m4a) format
                            if (err.number === -1072868846) {
                                // first we clear existing timeout to prevent success callback to be called with invalid arguments
                                // second we start same actions to try to record m4a audio
                                clearTimeout(stopRecordTimeout);
                                localAppData.createFileAsync('captureAudio.m4a', generateUniqueName).then(function (storageFile) {
                                    capturedFile = storageFile;
                                    mediaCapture.startRecordToStorageFileAsync(m4aEncodingProfile, capturedFile).then(
                                        function () {
                                            stopRecordTimeout = setTimeout(stopRecord, audioOptions.duration * 1000);
                                        },
                                        function () {
                                            // if we here, we're totally failed to record either mp3 or m4a
                                            errorCallback(new CaptureError(CaptureError.CAPTURE_INTERNAL_ERR));
                                        }
                                    );
                                });
                            } else {
                                errorCallback(new CaptureError(CaptureError.CAPTURE_INTERNAL_ERR));
                            }
                        }
                    );
                },
                function () {
                    errorCallback(new CaptureError(CaptureError.CAPTURE_NO_MEDIA_FILES));
                }
            );
        });
    },