React Native 无法开始录制,[TypeError:undefined不是对象(评估'_ExponentAV.default. requestPermissionsAsync')]

nuypyhwy  于 2022-12-14  发布在  React
关注(0)|答案(1)|浏览(122)

当我使用expo audio录制音频时,总是出现错误:无法开始录制,TypeError:undefined不是评估'_ExponentAV.default.requestPermissionsAsync'的对象,位于node_modules\expo\build\environment\react-native-logs.fx.js:27:23,在启动记录中的src\components\Chat\ChatForm.tsx:189:19发生错误

import React, { useState } from "react";

import { Audio } from "expo-av";

type ChatFormProps = {
  onSend: () => void;
};

const ChatForm: React.FC<ChatFormProps> = ({ onSend }) => {

const [isRecording, setIsRecording] = useState(false);
const [recordTime, setRecordTime] = useState<string>("00:00");

const recording = new Audio.Recording();

  const startRecording = async () => {
    try {
      console.log("Requesting permissions..");
      // console.log(Audio);
      await Audio.requestPermissionsAsync();
      await Audio.setAudioModeAsync({
        allowsRecordingIOS: true,
        playsInSilentModeIOS: true,
      });

      console.log("Starting recording..");
      await recording.prepareToRecordAsync(
        Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY
      );
      await recording.startAsync();
      setIsRecording(!isRecording);
      console.log("Recording started");
    } catch (err) {
      console.error("Failed to start recording", err);
    }
  };

  const stopRecording = async () => {
    try {
      console.log("Stopping recording..");
      setIsRecording(!isRecording);
      await recording.stopAndUnloadAsync();
      const uri = recording.getURI();
      console.log("Recording stopped and stored at", uri);
    } catch (err) {
      console.error("Failed to stop recording", err);
    }
  };
}
tvmytwxo

tvmytwxo1#

尝试初始化try块内的记录

const startRecording = async () => {
    try {
      console.log("Requesting permissions..");
      // console.log(Audio);
      await Audio.requestPermissionsAsync();
      await Audio.setAudioModeAsync({
        allowsRecordingIOS: true,
        playsInSilentModeIOS: true,
      });

const recording = new Audio.Recording();

      console.log("Starting recording..");
      await recording.prepareToRecordAsync(
        Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY
      );
      await recording.startAsync();
      setIsRecording(!isRecording);
      console.log("Recording started");
    } catch (err) {
      console.error("Failed to start recording", err);
    }
  };

  const stopRecording = async () => {
    try {
      console.log("Stopping recording..");
      setIsRecording(!isRecording);
      await recording.stopAndUnloadAsync();
      const uri = recording.getURI();
      console.log("Recording stopped and stored at", uri);
    } catch (err) {
      console.error("Failed to stop recording", err);
    }
  };
}

相关问题