Question: package com.example.speechtotext; import android.Manifest; import android.content.pm . PackageManager; import android.os . Bundle; import android.content.Intent; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; import android.widget.Button; import android.widget.TextView; import
package com.example.speechtotext;
import android.Manifest;
import android.content.pmPackageManager;
import android.osBundle;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.media.MediaPlayer;
import java.ioIOException;
import java.util.ArrayList;
import java.util.Locale;
import android.util.Log;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
public class MainActivity extends AppCompatActivity
TextView transcriptionTextView;
SpeechRecognizer speechRecognizer;
Intent speechRecognizerIntent;
MediaPlayer mediaPlayer;
@Override
protected void onCreateBundle savedInstanceState
super.onCreatesavedInstanceState;
setContentViewRlayout.activitymain;
transcriptionTextView findViewByIdRidtranscriptionTextView;
speechRecognizer SpeechRecognizer.createSpeechRecognizerthis;
speechRecognizerIntent new IntentRecognizerIntentACTIONRECOGNIZESPEECH;
speechRecognizerIntent.putExtraRecognizerIntentEXTRALANGUAGEMODEL, RecognizerIntent.LANGUAGEMODELFREEFORM;
speechRecognizerIntent.putExtraRecognizerIntentEXTRALANGUAGE, Locale.getDefault;
speechRecognizer.setRecognitionListenernew RecognitionListener
@Override
public void onReadyForSpeechBundle params
@Override
public void onBeginningOfSpeech
@Override
public void onRmsChangedfloat rmsdB
@Override
public void onBufferReceivedbyte buffer
@Override
public void onEndOfSpeech
@Override
public void onErrorint error
@Override
public void onResultsBundle results
ArrayList matches results.getStringArrayListSpeechRecognizerRESULTSRECOGNITION;
if matches null && matches.isEmpty
String recognizedText matches.get;
Log.dSpeechRecognition "Recognized text: recognizedText;
transcriptionTextView.setTextrecognizedText;
@Override
public void onPartialResultsBundle partialResults
@Override
public void onEventint eventType, Bundle params
;
Button startButton findViewByIdRidstartButton;
Button stopButton findViewByIdRidstopButton;
Button playButton findViewByIdRidplayButton;
startButton.setOnClickListenerv startRecording;
stopButton.setOnClickListenerv stopRecording;
playButton.setOnClickListenerv startPlayback;
mediaPlayer new MediaPlayer;
private void startRecording
if ContextCompatcheckSelfPermissionthis Manifest.permission.RECORDAUDIO
PackageManager.PERMISSIONGRANTED
speechRecognizer.startListeningspeechRecognizerIntent;
else
ActivityCompat.requestPermissionsthis new StringManifestpermission.RECORDAUDIO;
private void stopRecording
speechRecognizer.stopListening;
private void startPlayback
try
AssetManager assetManager getAssets;
AssetFileDescriptor descriptor assetManager.openFdaudiosample.amr";
mediaPlayer.setDataSourcedescriptorgetFileDescriptor descriptor.getStartOffset descriptor.getLength;
mediaPlayer.prepare;
mediaPlayer.start;
catch IOException e
Log.eMainActivity "Error starting playback", e;
@Override
public void onRequestPermissionsResultint requestCode, @NonNull String permissions, @NonNull int grantResults
super.onRequestPermissionsResultrequestCode permissions, grantResults;
if requestCode
if grantResultslength && grantResults PackageManager.PERMISSIONGRANTED
startRecording;
@Override
protected void onDestroy
super.onDestroy;
if mediaPlayer null
mediaPlayer.release;
this is my mainactivity.java code for speechtotext application in android studio, but I am unable to see the transcription of the audio file when it is played kindly help me with this and make the necessay changes in this code to get the transcription of the audio and make this code unique and error free with explnation so that I can get the desired output
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
