How to create a personal voice assistant Android App to create a text file? - complete source code

Published: 21 March 2020
on channel: Programmer World
22,498
381

In this video it shows the steps to create a personal voice assistant Android App.

It shows a very basic logic in which a file is created by the App automatically whenever a user says create to it.

The App uses TextToSpeech API to speak to the user for the inputs it requires.

SpeehRecognizer API is used to capture the inputs spoken by the user.

The complete source code and test results of this tutorial is available on the below webpage:
https://programmerworld.co/android/ho...

For any suggestions, queries and appreciation please connect with us at:
https://programmerworld.co/contact/ or [email protected]

Java code is available below:

package com.example.mypersonalvoiceassitance;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {

private SpeechRecognizer speechRecognizer;
private TextToSpeech textToSpeech;
private TextView textView;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
textView = findViewById(R.id.textView);
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
}
});

intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
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 ANGULAR_BRACKET String ANGULAR_BRACKET matches = results.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
String string = "";
textView.setText("");
if (matches != null){
string = matches.get(0);
textView.setText(string);

if (string.equals("create")){
createMethod();
}
}
}
@Override
public void onPartialResults(Bundle partialResults) {
}
@Override
public void onEvent(int eventType, Bundle params) {
}
});
}

public void startButton(View view){
textToSpeech.speak("Please tell me, how can I help you?", TextToSpeech.QUEUE_FLUSH, null, null);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
speechRecognizer.startListening(intent);
}

private void createMethod(){
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "PersonalAssistant.txt");
try {
if (!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
fileWriter.append("My 1st Personal voice assistance App development");
fileWriter.flush();
fileWriter.close();
}
catch (Exception e){
e.printStackTrace();
return;
}
textToSpeech.speak("The text file has been created. Thank you for using my service.", TextToSpeech.QUEUE_FLUSH, null, null);
}
}