首先,您在获得录音后将您的状态分配给undefined。
我建议你做这样的事情
为Recording 和Playing 音频创建两个引用。
const AudioRecorder = useRef(new Audio.Recording());
const AudioPlayer = useRef(new Audio.Sound());
还有recordingStatus、permission 等的一些状态。
const [RecordedURI, SetRecordedURI] = useState<string>("");
const [AudioPermission, SetAudioPermission] = useState<boolean>(false);
const [IsRecording, SetIsRecording] = useState<boolean>(false);
const [IsPLaying, SetIsPLaying] = useState<boolean>(false);
录制实现的小吃是here
这是一个GitHub Repo,它有实现。
Screenshot 的结果
我在其余的实现下面添加了
import React, { useState, useRef, useEffect } from "react";
import { View, StyleSheet, Button, Text } from "react-native";
import { Audio } from "expo-av";
export default function App() {
// Refs for the audio
const AudioRecorder = useRef(new Audio.Recording());
const AudioPlayer = useRef(new Audio.Sound());
// States for UI
const [RecordedURI, SetRecordedURI] = useState<string>("");
const [AudioPermission, SetAudioPermission] = useState<boolean>(false);
const [IsRecording, SetIsRecording] = useState<boolean>(false);
const [IsPLaying, SetIsPLaying] = useState<boolean>(false);
// Initial Load to get the audio permission
useEffect(() => {
GetPermission();
}, []);
// Function to get the audio permission
const GetPermission = async () => {
const getAudioPerm = await Audio.requestPermissionsAsync();
SetAudioPermission(getAudioPerm.granted);
};
// Function to start recording
const StartRecording = async () => {
try {
// Check if user has given the permission to record
if (AudioPermission === true) {
try {
// Prepare the Audio Recorder
await AudioRecorder.current.prepareToRecordAsync(
Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY
);
// Start recording
await AudioRecorder.current.startAsync();
SetIsRecording(true);
} catch (error) {
console.log(error);
}
} else {
// If user has not given the permission to record, then ask for permission
GetPermission();
}
} catch (error) {}
};
// Function to stop recording
const StopRecording = async () => {
try {
// Stop recording
await AudioRecorder.current.stopAndUnloadAsync();
// Get the recorded URI here
const result = AudioRecorder.current.getURI();
if (result) SetRecordedURI(result);
// Reset the Audio Recorder
AudioRecorder.current = new Audio.Recording();
SetIsRecording(false);
} catch (error) {}
};
// Function to play the recorded audio
const PlayRecordedAudio = async () => {
try {
// Load the Recorded URI
await AudioPlayer.current.loadAsync({ uri: RecordedURI }, {}, true);
// Get Player Status
const playerStatus = await AudioPlayer.current.getStatusAsync();
// Play if song is loaded successfully
if (playerStatus.isLoaded) {
if (playerStatus.isPlaying === false) {
AudioPlayer.current.playAsync();
SetIsPLaying(true);
}
}
} catch (error) {}
};
// Function to stop the playing audio
const StopPlaying = async () => {
try {
//Get Player Status
const playerStatus = await AudioPlayer.current.getStatusAsync();
// If song is playing then stop it
if (playerStatus.isLoaded === true)
await AudioPlayer.current.unloadAsync();
SetIsPLaying(false);
} catch (error) {}
};
return (
<View style={styles.container}>
<Button
title={IsRecording ? "Stop Recording" : "Start Recording"}
color={IsRecording ? "red" : "green"}
onPress={IsRecording ? StopRecording : StartRecording}
/>
<Button
title={IsPLaying ? "Stop Sound" : "Play Sound"}
color={IsPLaying ? "red" : "orange"}
onPress={IsPLaying ? StopPlaying : PlayRecordedAudio}
/>
<Text>{RecordedURI}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
backgroundColor: "#ecf0f1",
padding: 8,
},
});