Я пытаюсь выполнить распознавание речи в режиме реального времени на Android путем потоковой передачи звука с микрофона с android на мой сервер API с помощью веб-сокета и получения возврата текста. Я уже сделал это в веб-версии нашего проекта, написанной на React:
start = () => {
let audioContext = null;
let recorder = null;
if (!audioContext) {
audioContext = new(window.AudioContext || window.webkitAudioContext)();
if (audioContext.state === 'suspended') {
audioContext.resume();
}
navigator.mediaDevices.getUserMedia({
audio: true
})
.then((stream)=> {
var audioInput = audioContext.createMediaStreamSource(stream);
var bufferSize = 2048;
recorder = audioContext.createScriptProcessor(bufferSize, 1, 1);
recorder.onaudioprocess = (e)=> {
if (!this.state.isStop && this.ws && this.ws.readyState === this.ws.OPEN) {
let buffer = e.inputBuffer.getChannelData(0);
let int16ArrayData = convertFloat32ToInt16(buffer);
this.ws.send(int16ArrayData.buffer);
}
};
audioInput.connect(recorder);
recorder.connect(audioContext.destination);
}).catch(function(e) {
console.log("Error when getUserMedia");
console.log(e);
});
}
this.ws= new WebSocket(`${API_URL}?content-type=audio/x-raw,+layout=(string)interleaved,+rate=(int)${audioContext.sampleRate},+format=(string)S16LE,+channels=(int)1&model=youtube&token=${token}`);
this.ws.onopen = () => {
console.log("Opened connection to websocket " + API_URL);
this.setState({isStop:false})
};
this.ws.onclose = () => {
console.log("Websocket closed");
this.stop();
};
this.ws.onmessage = (e) => {
var resp = JSON.parse(e.data);
this.processJsonResponse(resp);
};
}
processJsonResponse = (resp)=> {
if (resp.status === 0 && resp.result && resp.result.hypotheses.length > 0) {
var text = resp.result.hypotheses[0].transcript_normed || resp.result.hypotheses[0].transcript;
if (text === '<unk>.') {
return;
}
if (text.endsWith('.')) {
text = text.slice(0, -1);
}
if (resp.result.final) {
this.setState({text:[...this.state.text,this.toUpperFirstChar(text)],temp:''})
} else {
this.setState({temp:text})
}
}
}
toUpperFirstChar(text){
return text.charAt(0).toUpperCase() + text.substring(1)
}
getSummary = (text) =>{
axios.post('http://34.70.94.164:5000/summarize?ratio=0.1', text, {
headers: { 'Content-Type': 'text/plain' }
}).then(function(response) {
this.setState({summary:response.data.summary})
})
.catch(function(error) {
console.log(error);
});
}
Но теперь мне нужно сделать это на Android, но я нашел способ записи звука только в Android (Я здесь новенький). Пожалуйста, предложите мне способ сделать это, как в реакции Реакция ссылка: Github Project Link