我尝试在iOS上使用由create-react-native-app创建的React Native应用程序录制音频。我正在使用expo-av库进行录制。但是,当我按下“开始录制”时,它立即崩溃。通过一些console.log语句,我发现它发生在“请求权限”之后,但在“开始录制”之前。这在Android上运行良好。附件是我的代码。为什么会发生这种情况?
import React, {useEffect, useState, setState} from 'react';
import {AsyncStorage, FlatList} from 'react-native';
import { Button, Layout, Text, Card } from '@ui-kitten/components';
import styles from './../../util/styles';
import {Audio} from 'expo-av';
import RNFS from 'react-native-fs';
export default function Record({navigation}) {
const [recording, setRecording] = useState();
const REC_OPT = {
isMeteringEnabled: true,
android: {
extension: '.m4a',
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
sampleRate: 16000,
numberOfChannels: 1,
bitRate: 128000,
},
ios: {
extension: '.wav',
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_MAX,
sampleRate: 16000,
numberOfChannels: 1
},
};
async function startRecording() {
try {
console.log('Requesting permissions... ');
await Audio.requestPermissionsAsync();
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true,
});
console.log('Starting recording..');
const recording = new Audio.Recording();
await recording.prepareToRecordAsync(REC_OPT);
await recording.startAsync();
setRecording(recording);
console.log('Recording started');
} catch (err) {
console.error('Failed to start recording', err);
}
}
async function stopRecording() {
console.log('Stopping recording..');
setRecording(undefined);
await recording.stopAndUnloadAsync();
const uri = recording.getURI();
console.log('Recording stopped and stored at', uri);
const fileArray = uri.split("/");
const filename = fileArray[fileArray.length - 1];
const newFilepath = RNFS.DocumentDirectoryPath + "/" + filename;
console.log("old filepath: ", uri, " new filepath: ", filename, " document directory ", RNFS.DocumentDirectoryPath, " file array ", fileArray, " file array length ", fileArray.length, " fullFilepath ", newFilepath);
await RNFS.copyFile(uri, newFilepath);
try {
var value = await AsyncStorage.getItem('FILES');
if(value !== null){
console.log("existing data");
} else {
value = "{}";
}
var rString = randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
var json = JSON.parse(value);
json[rString] = {"file_path": newFilepath, "name": rString, "id": rString, "analyzed": false, "analysis": {}};
var jsonString = JSON.stringify(json);
// console.log("update file: FILES " + jsonString);
await AsyncStorage.setItem("FILES", jsonString);
navigation.navigate('Home', {refresh: true});
} catch (error) {
console.error("failed to start recording", error);
}
}
randomString = (length, chars) => {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
return (
<Layout style={styles.container}>
<Button
onPress={recording ? stopRecording : startRecording}
>
{recording ? 'Stop Recording' : 'Start Recording'}
</Button>
</Layout>
);
}
1条答案
按热度按时间bxgwgixi1#
你能试着在你的info.plist文件中添加这些行吗
您的info.plist文件应如下所示