LocalFileSystem.PERSISTENT 音频文件不会在 Cordova 中保留

Audio file does not persist in Cordova with LocalFileSystem.PERSISTENT

我已经尝试将音频文件存储在持久存储中两天了,但没有成功。

到目前为止,我可以使用下面附带的代码创建一个音频文件来记录来自麦克风的音频(该应用程序具有权限)。

音频文件生成存储成功,可以播放

但真正的问题是当我关闭应用程序并返回并尝试播放它显示错误的文件时。

"{"message": "Cannot use audio file from resource '/myrecording.wav'", "code":1}"

即使我使用 LocalFileSystem.PERSISTENT.

,该文件也不会跨应用程序会话持久保存

我不确定问题出在我的 Media/Audio 代码还是文件存储代码上。

请找到下面附上的代码:

下面的函数记录了来自麦克风的音频。

function _recordAudio() {
        var deferred = $q.defer();
        var src = "myrecording.wav";
        alert("SRC:" + src);
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
            fileSystem.root.getFile(src, {
                create: true,
                exclusive: false
            }, function (fileEntry) {
                alert("File " + src + " created at " + fileEntry.fullPath);
                var mediaRec = new Media(fileEntry.fullPath,
                    function () {
                        alert("Success");
                    }, function (error) {
                        alert("error:" + JSON.stringify(error));
                    });
                // Record audio
                mediaRec.startRecord();

                // Stop recording after 10 sec
                var recTime = 0;
                var recInterval = setInterval(function () {
                    recTime = recTime + 1;
                    if (recTime >= 5) {
                        clearInterval(recInterval);
                        mediaRec.stopRecord();
                        deferred.resolve(fileEntry.fullPath);
                    }
                }, 1000);
            }, function (error) {
                alert("getFile error:" + JSON.stringify(error));
                deferred.reject();
            }); //of getFile
        }, function (error) {
            alert("requestFileSystem error:" + JSON.stringify(error));
            deferred.reject();
        }); //of requestFileSystem
        return deferred.promise;
    }

下面的函数播放音频。

    function _play2() {
        var src = "myrecording.wav";
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
            fileSystem.root.getFile(src, null, function (fileEntry) {
                alert("File " + src + " created at " + fileEntry.fullPath);
                var mediaRec = new Media(fileEntry.fullPath,
                    function () {
                        alert("Success play2");
                    }, function (error) {
                     //Getting error after closing and opening the app
                     //Error message = {"message": "Cannot use audio file from resource '/myrecording.wav'","code":1}
                        alert("error play2:" + JSON.stringify(error));
                    });

                mediaRec.play();
                });
        });

    }

我通过将 cdvfile: 路径传递给 PlayAudio 函数代码中的媒体插件并将文件从临时存储复制 到持久存储。 我不得不使用文件的 localURL。 这部分解决了我的问题:

fileEntry.file(function (file) {
                            _playNow(file.localURL);
                        }

有关完整功能,请参阅下面的代码片段:

recordAudio: function (projectNo, ItemNo) {
                try {
                    var deferred = $q.defer();    

                    var recordingTime = 0;
                    _audioLoader = $("#audioLoader");
                    _audioLoader.show();
                    UtilityService.showPopup('audio');
                    _isRecording = true;

                    _recordFileName = "Audio_" + projectNo + "_" + ItemNo + ".wav";
                    _mediaRecord = new Media(_recordFileName);

                    //Record audio
                    _mediaRecord.startRecord();

                    var recordingInterval = setInterval(function () {
                        recordingTime = recordingTime + 1;

                        $('#audioPosition').text(_secondsToHms(recordingTime));

                        if (!_isRecording) {
                            clearInterval(recordingInterval);
                            _mediaRecord.stopRecord();
                            _mediaRecord.release();

                            deferred.resolve();
                        }
                    }, 1000);

                    //document.getElementById('audioPosition').innerHTML = '0 sec';
                    $('#audioPosition').text('0 sec');
                    return deferred.promise;
                }
                catch (ex) {
                    alert('WMMCPA|recordAudio:- ' + ex.message);
                }
            },

从持久存储中获取文件路径并将其发送到播放方法。

            //To play recorded audio for specific project item 
            playAudio: function (projectNo, ItemNo) {
                try {
                    _recordFileName = "Audio_" + projectNo + "_" + ItemNo + ".wav";

                    var newFileUri = cordova.file.dataDirectory + _recordFileName;
                    window.resolveLocalFileSystemURL(newFileUri, function (fileEntry) {
                        fileEntry.file(function (file) {
                            _playNow(file.localURL);
                        }, function (error) {
                            alert("WMMCPA|playAudio.file:-" + JSON.stringify(error));
                        });

                    }, function (error) {
                        alert("WMMCPA|playAudio.resolveLocalFileSystemURL:-" + JSON.stringify(error));
                    });
                }
                catch (ex) {
                    alert("WMMCPA|playAudio:-" + ex.message);
                }
            }
function _playNow(src) {
            try {
                var mediaTimer = null;

                _audioLoader = $("#audioLoader");
                _audioLoader.show();
                UtilityService.showPopup('audio');

                //Create Media object from src
                _mediaRecord = new Media(src);

                //Play audio
                _mediaRecord.play();

                    } catch (ex) {
                        alert('WMMCPA|_playNow.mediaTimer:- ' + ex.message);
                    }
                }, 1000);

            } catch (ex) {
                alert('WMMCPA|_playNow:- ' + ex.message);