节点后端的 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 空响应
发布评论