带有节点后端的 React Native Expo 应用程序中的 GCP 空响应

编程入门 行业动态 更新时间:2024-10-04 17:18:38

带有<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点后端的 React Native Expo 应用程序中的 GCP 空响应"/>

带有节点后端的 React Native Expo 应用程序中的 GCP 空响应

我一直在尝试使用 GCP 将语音转换为文本。我在 IOS 模拟器上使用来自 expo-av 的音频,但我一直从 API 收到空响应。

这是我的前端函数,它将数据发送到我的节点后端

async function startRecording() {
    if (audioPermission == "granted") {
      await Audio.setAudioModeAsync({
        allowsRecordingIOS: true,
        playsInSilentModeIOS: true
      })
    }
    try {
      const newRecording = new Audio.Recording();
      console.log('Starting Recording')
      await newRecording.prepareToRecordAsync();
      await newRecording.startAsync();
      setRecording(newRecording);
      setRecordingStatus('recording');
    } catch (error) {
      console.error('Failed to start recording', error);
    }
  }

  async function stopRecording() {
    try {
      if (recordingStatus === 'recording') {
        await recording.stopAndUnloadAsync();
        const recordingUri = recording.getURI();
        console.log(recordingUri)
        const fileName = `recording-${Date.now()}.caf`;
        await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'recordings/', { intermediates: true });
        const filePath = FileSystem.documentDirectory + 'recordings/' + `${fileName}`
        await FileSystem.moveAsync({
          from: recordingUri,
          to: filePath
        });
        console.log(filePath)
        uploadAudio(filePath)
        const playbackObject = new Audio.Sound();
        await playbackObject.loadAsync({ uri: filePath});
        await playbackObject.playAsync();
        setRecording(null);
        setRecordingStatus('stopped');
      }
    } catch (error) {
      console.error('Failed to stop recording', error);
    }
  }

  async function handleRecordButtonPress() {
    if (recording) {
      const audioUri = await stopRecording(recording);
    } else {
      await startRecording();
    }
  }

我点击一个触发 handleRecordButtonPress() 的按钮,它触发开始和停止按钮。

这是我处理文件的后端代码

const client = new speech.SpeechClient({
    keyFilename: 'config/key.json',
});
const app = express();

// Set up multer middleware to handle file uploads
const upload = multer({
  storage: multer.memoryStorage(),
  limits: {
    fileSize: 10 * 1024 * 1024, // Maximum file size is 10 MB
  },
});


async function sendSpeechToGCP(file) {
    try {
        // Send the audio file to the Speech-to-Text API for transcription
        const [response] = await client.recognize({
            audio: {
                content: file.buffer,
            },
            config: {
                encoding: 'FLAC',
                sampleRateHertz: 16000,
                languageCode: 'en-US',
            },
        })
        console.log(response)
        const transcription = response.results.map(result => 
            result.alternatives[0].transcript).join('\n');
        console.log(`Transcription: ${transcription}`);
    }   
    catch (error) {
        console.error(error);
    }
}

我认为问题可能是前端库仅记录在 .caf 扩展名中,这是有问题的。有办法解决这个问题吗?任何意见,将不胜感激。如果是这样的话,expo 图书馆不是最好的。

我知道音频很好,因为我可以播放它并在我的模拟器上听到它 FYI。

回答如下:

更多推荐

带有节点后端的 React Native Expo 应用程序中的 GCP 空响应

本文发布于:2024-05-30 05:33:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   应用程序   后端   Native   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!