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.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 androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.media.MediaPlayer;
import java.io.IOException;
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 onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
transcriptionTextView = findViewById(R.id.transcriptionTextView);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechRecognizer.setRecognitionListener(new RecognitionListener(){
@Override
public void onReadyForSpeech(Bundle params){}
@Override
public void onBeginningOfSpeech(){}
@Override
public void onRmsChanged(float rmsdB){}
@Override
public void onBufferReceived(byte[] buffer){}
@Override
public void onEndOfSpeech(){}
@Override
public void onError(int error){}
@Override
public void onResults(Bundle results){
ArrayList matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null && !matches.isEmpty()){
String recognizedText = matches.get(0);
Log.d("SpeechRecognition", "Recognized text: "+ recognizedText);
transcriptionTextView.setText(recognizedText);
}
}
@Override
public void onPartialResults(Bundle partialResults){}
@Override
public void onEvent(int eventType, Bundle params){}
});
Button startButton = findViewById(R.id.startButton);
Button stopButton = findViewById(R.id.stopButton);
Button playButton = findViewById(R.id.playButton);
startButton.setOnClickListener(v -> startRecording());
stopButton.setOnClickListener(v -> stopRecording());
playButton.setOnClickListener(v -> startPlayback());
mediaPlayer = new MediaPlayer();
}
private void startRecording(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_GRANTED){
speechRecognizer.startListening(speechRecognizerIntent);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO},1);
}
}
private void stopRecording(){
speechRecognizer.stopListening();
}
private void startPlayback(){
try {
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("audio_sample.amr");
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e){
Log.e("MainActivity", "Error starting playback", e);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode ==1){
if (grantResults.length >0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!