#javascript #android #react-native #expo #google-speech-api
#javascript #Android #react-родной #expo #google-speech-api
Вопрос:
Я пытаюсь записать аудио на Expo и получить его транскрипцию с помощью службы преобразования речи в текст Google.
Он уже работает на iOS, но еще не на Android. Я думаю, что это проблема с параметрами записи для Android.
Я не получал ответы об ошибках с сервера Google, только пустой объект.
Вот код:
const recordingOptions = {
// android not currently in use, but parameters are required
android: {
extension: ".m4a",
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_MPEG_4,
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AAC,
sampleRate: 44100,
numberOfChannels: 1,
bitRate: 128000,
},
ios: {
extension: ".wav",
audioQuality: Audio.RECORDING_OPTION_IOS_AUDIO_QUALITY_HIGH,
sampleRate: 44100,
numberOfChannels: 1,
bitRate: 128000,
linearPCMBitDepth: 16,
linearPCMIsBigEndian: false,
linearPCMIsFloat: false,
},
};
const [recording, setRecording] = useState<Audio.Recording | null>(null);
const startRecording = async () => {
const { status } = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
if (status !== "granted") return;
// some of these are not applicable, but are required
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
playThroughEarpieceAndroid: true,
});
const newRecording = new Audio.Recording();
try {
await newRecording.prepareToRecordAsync(recordingOptions);
await newRecording.startAsync();
} catch (error) {
console.log(error);
stopRecording();
}
setRecording(newRecording);
};
const stopRecording = async () => {
try {
await recording!.stopAndUnloadAsync();
} catch (error) {
// Do nothing -- we are already unloaded.
}
};
const getAudioTranscription = async () => {
try {
const info = await FileSystem.getInfoAsync(recording!.getURI()!);
console.log(`FILE INFO: ${JSON.stringify(info)}`);
const uri = info.uri;
await toDataUrl(uri, async function (base64content: string) {
if (Platform.OS == "ios")
base64content = base64content.replace("data:audio/vnd.wave;base64,", "");
else
base64content = base64content.replace("data:audio/aac;base64,", "");
console.log(recording?._options?.android)
const body = {
audio: {
content: base64content,
},
config: {
enableAutomaticPunctuation: true,
encoding: "LINEAR16",
languageCode: "pt-BR",
model: "default",
sampleRateHertz: 44100,
},
};
const transcriptResponse = await fetch(
"https://speech.googleapis.com/v1p1beta1/speech:recognize?key=MY_KEY",
{ method: "POST", body: JSON.stringify(body) }
);
const data = await transcriptResponse.json();
const userMessage = data.results amp;amp; data.results[0].alternatives[0].transcript || "";
});
} catch (error) {
console.log("There was an error", error);
}
stopRecording();
};
Ответ №1:
Эта комбинация определенно работает, хотя у меня было много других проблем с поведением Expo до того, как я пришел к такому выводу.
extension: '.amr',
outputFormat: Audio.RECORDING_OPTION_ANDROID_OUTPUT_FORMAT_AMR_WB,
audioEncoder: Audio.RECORDING_OPTION_ANDROID_AUDIO_ENCODER_AMR_WB,
sampleRate: 16000,
numberOfChannels: 1,
bitRate: 128000,