[ad_1]
Hello, I’m trying to implement a Speech-to-text function in the plugin via Open AI Whisper.
function recording_shortcode() {
ob_start();
?>
<style>
#start-recording {
position: fixed;
margin: auto;
padding: 10px;
bottom: 150px;
right: 0px;
width: 30%;
cursor: pointer;
z-index: 200000002;
display: block !important;
justify-content: flex-start !important;
background: linear-gradient(to right, #FFF7AD, #FFA9F9);
border-color: #FFA9F9 !important;
color: #000 !important;
border-radius: 25px !important;
font-size: 16px !important;
font-weight: 500 !important;
letter-spacing: 0.5px !important;
margin-left: 10px !important;
}
#stop-recording {
display: none;
}
.stop-recording {
position:fixed;
top:50%;
left:50%;
object-fit: cover; /* Ensures the image covers the entire element without distortion */
object-position: center;
transform:translate(-50%, -50%);
padding:0;
background-color:white;
border:1px solid #ccc;
width: 100%;
height: 100%;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
z-index: 200000002;
}
</style>
<button id="start-recording"><span class="dashicons dashicons-microphone"></span>Speak</button>
<img src="https://projectdmc.org/support/topic/send-button-is-not-triggered-if-i-change-the-value-of-the-textarea-with-js/imagerurl.com" id="stop-recording" class="stop-recording" />
<script>
let chunks = [];
let mediaRecorder;
let recordingTimeout;
const startRecording = document.getElementById('start-recording');
const stopRecording = document.getElementById('stop-recording');
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = event => {
chunks.push(event.data);
};
mediaRecorder.onstart = () => {
recordingTimeout = setTimeout(() => mediaRecorder.stop(), 15000);
};
mediaRecorder.onstop = async () => {
clearTimeout(recordingTimeout);
const blob = new Blob(chunks, { type: 'audio/mp3' });
chunks = [];
let formData = new FormData();
formData.append('file', blob);
formData.append('model', 'whisper-1');
const response = await fetch('https://api.openai.com/v1/audio/transcriptions', {
method: 'POST',
headers: {
'Authorization': 'Bearer API KEY'
},
body: formData
});
const data = await response.json();
document.querySelectorAll('textarea').forEach(textarea => {
textarea.value = data.text ? data.text : "Transcription not available";
});
};
startRecording.addEventListener('click', () => {
if (mediaRecorder.state !== 'recording') {
mediaRecorder.start();
startRecording.style.display = 'none';
stopRecording.style.display = 'block';
}
});
stopRecording.addEventListener('click', () => {
if (mediaRecorder.state === 'recording') {
mediaRecorder.stop();
stopRecording.style.display = 'none';
startRecording.style.display = 'block';
}
});
}).catch(error => {
console.error('Error:', error);
});
</script>
<?php
return ob_get_clean();
}
add_shortcode('recording_shortcode', 'recording_shortcode');
What this code does is that it transcribes my audio that I recorded with microphone and fills transcription in the textarea. But the problem is, after the transcription is put inside the text-area, the send button of the plugin is not visible. I need to type something in my physical keyboard in the text area to trigger send button visibility. Any idea how I can achieve what I want to achieve?
Thanks for the help in advance.