How To Build An Audio To Text Convertor Android App - Hive Programmers
Greetings to my favorite Science community online, StemSocial.
It's @skyehi and I'm super excited to be back to continue my series on Android App Development Tutorials for beginners.
Yesterday we had a really different kind of episode and I taught on how to build a Hangman game using Python. I promised my readers I'll be getting back on track with out series and I'm here to do just that.
Original Image Source by Pixabay from Pexels
For today's episode we're going to be learning how to build a convertor Android App. An App that would allow us to convert audio to text.
There are amazing and useful Apps like that online. You can easily record yourself talk and it would translate the audio into written text which you cn use for anything.
These Apps are very helpful for those days where you're feeling too lazy to type, or you've typed so much that your fingers are just tired and you can't continue or when you're in a situation where it's not safe to type.
For example, while you're driving and you get any idea which you want to write down. You can easily use Apps like these to record your voiceovers and it would be written for you in plain text.
I hope you're excited about this episode, we're going to learn how such Apps are built. Let's get started with our work shall we
Prerequisites
As always guys for the newcomers, you would need to install two primary softwares on your PC to be able to successfully go through with this tutorial.
Android Studio IDE which is the platform on which we'll build our Android App
Java Development Kit, JDK which is required for your PC to be able to execute Java codes or instructions
If you're having troubles installing any of these softwares please let me know in the comments.
If you're through with this stage, you're definitely ready to follow this tutorial guys
Creating A New Android Project
Now guy, go ahead and open Android studio and click on "Create A New Android project"
As always, please select Empty Activity as the template of our project. Click on next and set both the App name and package name of your Audio to Text Convertor App.
I would definitely recommend that you choose a unique name if you intend to publish this on Google Play Store because there are quite a lot of Audio to text convertors out there and you would want your App to stand out.
Also guys, ensure that Java is the selected programming language and not Kotlin. If Kotlin is preselected, please change it because we're be writing our codes in Java and not Kotlin.
When you're through with the configuration of your App Project, please click on the "Finish" button, sit back and let Android Studio set up your new App project space for you.
Implementing The Dependencies
Now guys we talked a lot about the importance of dependencies when you want to include some services or libraries in your project.
Since we'll need a couple of useful services in our Android project, head over to the Gradle
file of your App, specifically the App level Gradle and include the following dependencies if they're not already there.
- Not doing this would give errors in your design and some backend codes as well
Here's how the gradle file code should look like
// Example build.gradle (Module: app) dependencies
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.android.gms:play-services:12.0.1'
When you're through with editing your Gradle file with these codes, a "Sync" button would appear, please click on it and Android Studio will Sync and download all necessary libraries into your Project.
User Interface (UI) Desig
Now that we're done with setting up our project, we can start with the frontend design or user interface of our Audio to Text Convertor App.
Since this is audio to text convertor, we would need two very important UI elements in our App;
A Button
element which would start a recoding for you to speaking and a TextView
element which would display the recorded texts.
It's pretty simple right guys. Of course you're totally free to modify it in any way and include more awesome features.
For the purpose of keeping this tutorial at beginner's level, I'll stick to a simple User Interface.
The design layout code for our App's User interface will be done inside the res/layout/activity_main.xml
file.
Here's how your code should look like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/recordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/recordButton"
android:layout_marginTop="16dp"/>
</RelativeLayout>
Handling Audio Input
Now guys the next thing we would want to do is to include an Audio recording feature.
We have built an entire Audio recorder App in this series before so I only went back to copy that code to avoid having to re-write the entire thing again.
If you're having troubles understand how this works, just let me know in the comments and I'll explain further guys.
But bottom line is that we'll be using the MediaRecorder
class which is already inbuilt into Android Studio to write the code of our Audio record feature.
This code will be written inside your MainActivity.java
file
Here's how your audio recording code should look like guys
// Example MainActivity.java
import android.media.MediaRecorder;
import android.os.Environment;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaRecorder mediaRecorder;
private String outputFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize MediaRecorder
mediaRecorder = new MediaRecorder();
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audio.3gp";
// Example code for starting recording
findViewById(R.id.recordButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(outputFile);
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
Converting The Recorded Audio to Text
Now guys, since we've successfully been able to implement a recording feature for the user to be able to record their voice or speech, it's time to implement the feature that would convert that audio data into a text.
I'm excited to introduce to my readers another new API in this series and that's the Android's SpeechRecognizer
API.
However guys you can totally use some external APIs like Google Cloud Speech-to-Text API to implement this Audio to Text feature in the App.
So some of the default classes we'll call will include ; RecognitionListener
, RecognizerIntent
and SpeechRecognizer
This happens to be a whole new API I'm introducing so I'll encourage my readers to really pay attention to it. Of course these are default in Android Studio so don't worry about errors showing up, however if errors start showing up, please let me know in the comments guys.
Here's how you use the audio recognition API to create an audio to text convertor feature
// Example code for implementing Speech-to-Text
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechRecognizer.setRecognitionListener(new RecognitionListener() {
@Override
public void onResults(Bundle results) {
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (matches != null && !matches.isEmpty()) {
String convertedText = matches.get(0);
// Update UI with converted text
((TextView) findViewById(R.id.resultTextView)).setText("Converted Text: " + convertedText);
}
}
// Implement other SpeechRecognizer methods as needed
});
// Start listening
speechRecognizer.startListening(recognizerIntent);
Like I stated in the codes, you are free to Implement other SpeechRecognizer methods as needed by the App.
Displaying Text Output
At this point, the main work is done guys. The last thing for us to do is to display the converted audio to text inside the TextView
element we created in our res/layout/activity_main.xml
file at the beginning.
While I was writing the audio convert code, I created a variable called convertedText
which holds the text gotten from the user's recording.
All we have to do right now is to call that variable inside a setText
method and the text will be displayed.
It's pretty simple and basic steps guys.
Here's how the code should look like and of course this would also be inside the MainActivity page.
// Example code for updating TextView with converted text
TextView resultTextView = findViewById(R.id.resultTextView);
resultTextView.setText("Converted Text: " + convertedText);
Request Permissions In Android Manifest
Now guys, the one thing we should never forget to do is to request access to some services in AndroidManfest
file.
Since we'll be using the audio recording service, we need our App to access the recording feature of the Android device by requesting RECORD_AUDIO
permission.
We also need to request a couple of storage permissions so our App can save the audio or the converted text.
Here's how the Permission request codes should look like guys
(html comment removed: Add these permissions inside the <manifest> tag )
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Running Our Audio To Text Convertor App
Congratulations guys, we have finally completed our App. Of course as Basic as it may look, it can be a foundation or a template to build more complex audio to text Apps.
I wonder if I should include text to audio App which essentially reads the text. Let me know if you guys want that in the next episode. 🤔
So you can run your app as usual either using your physical Android device or an emulator. When the App launches you should see a button which you can press to start recording your speech and it would be translated to text.
Thanks for taking the time to read today's blog guys. I hope you enjoyed this tutorial.
As we keep making progress this year, I'm positive that we'll get to advanced level before the end of the year guys. I hope you're excited with that news.
Have a Great Day and catch you next time on StemSocial. Goodbye 👨💻
Thanks everyone for the support to @stemsocial and my friends @stickupcurator or @stickupboys
You Can Follow Me @skyehi For More Like This And Others
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Thanks for including @stemsocial as a beneficiary, which gives you stronger support.