我使用Cordova File Plugin和Cordova Media Plugin捕获音频片段,并尝试将其保存在应用程序中。
这在iOS和某些 Android设备上正常运行。但是在许多(尤其是较旧的三星)设备上,捕获音频片段后,成功回调失败。
这是来自三星设备的相关错误信息:
from bs4 import BeautifulSoup
import urllib.request as request
import json
folder = r'./gallery'
URL = 'https://web.archive.org/web/20180324152250/http://www.awp.fr:80/project/poissy-galore-museum-visitors-center/'
response = request.urlopen(URL)
soup = BeautifulSoup(response, 'html.parser')
scriptCnt = soup.find('div', {'class': 'posts-wrapper'})
script = scriptCnt.find('script').text
json_str = script[str(script).find('var gallery_items = '):str(script).find(';')].replace('var gallery_items = ', '')
json_str = json.loads(json_str)
for item in json_str:
print(item['large'])
filename = item['large'].split('/')[-1]
req = request.Request(item['large'],
headers = {
'User-agent':
'Mozilla/5.0 (Windows NT 5.1; rv:43.0) Gecko/20100101 Firefox/43.0'})
resp = request.urlopen(req)
with open(filename, "wb") as fd:
fd.write(resp.read())
这是我们在记录文件后移动文件的方式:
temporary directory: file:///storage/emulated/0/Sounds/Spraak%20005.m4a
current file name: Spraak 005.m4a
move to this directory: file:///data/data/com.ctrl.appname/files/appname/recordings
new file name: opname_1539936956.m4a
cordova.js:309 Error in Success callbackId: File1062676118 : TypeError: e.getFile is not a function
cordova.js:311 Uncaught TypeError: e.getFile is not a function
at ng-cordova.min.js:8
at resolveLocalFileSystemURI.js:76
at Object.require.getFs (fileSystems-roots.js:41)
at success (resolveLocalFileSystemURI.js:70)
at Object.callbackFromNative (cordova.js:291)
at <anonymous>:1:9
有人知道为什么var rootDirectory = cordova.file.applicationStorageDirectory;
var recordingDirectory = "appname/recordings";
var filename = $scope.filename = 'opname_' + Math.floor(Date.now() / 1000);
var temp_dir = (ionic.Platform.isIOS()) ? cordova.file.tempDirectory : mediaFiles[i].fullPath.replace(mediaFiles[i].name, '');
var extension = mediaFiles[i].name.split('.')[1];
console.log('temporary directory: ' + temp_dir);
console.log('current file name: ' + mediaFiles[i].name);
console.log('move to this directory: ' + rootDirectory + recordingDirectory);
console.log('new file name: ' + filename + "." + extension);
$cordovaFile.moveFile(temp_dir, mediaFiles[i].name, rootDirectory + recordingDirectory, filename + "." + extension)
.then(function () {
database.add('Recordings', {0: filename, 1: 0}).then(function () {
});
$scope.entries.push({active: 0, name: filename});
$scope.alertNameChange = true;
}, function (error) {
alert(error);
console.dir(error);
});
会在Samsung设备上发生吗?注意:设备正在运行Android 5.0.1。这段代码在装有Android 8.0的华为手机上可以正常使用