user3385015
user3385015

Reputation: 97

Text-to-Speech Function not working

The Android does not talk despite my code being error free and accurate. I have used a toast function to see where the issue occurs. However, both toasts are called.

I have removed all irrelevant code.

The code below is mostly relevant.

package com.example.android.BluetoothChat;


import android.R.string;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;
import java.util.Set;
import android.bluetooth.BluetoothDevice;

import java.io.InputStream;
import java.io.OutputStream;
import android.bluetooth.BluetoothSocket;
import android.widget.EditText;
import java.io.IOException;
import java.util.UUID;

public class MainActivity extends Activity implements LocationListener{
    Button start, stop;
    TextView tv;
    TextView tv2;
    TextView sum;
    LocationManager lm;
    static TextToSpeech Talker;
    TextView myLabel;
    Handler mhandler = new Handler();
    int i;
    int sum1 = 0;
    static BluetoothDevice mmDevice;
    static BluetoothAdapter mBluetoothAdapter;
    static BluetoothSocket mmSocket;
    String lat;
    String lon;

    EditText myTextbox;
    static OutputStream mmOutputStream;
    static InputStream mmInputStream;
    static Thread workerThread;
    static byte[] readBuffer;
    static int readBufferPosition;
    static int counter;
    static volatile boolean stopWorker;
    TextToSpeech talker;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        setTitle("Seizure Detection Helmet Application");

        myTextbox = (EditText)this.findViewById(R.id.name);
        talker = new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener() {
            @Override

            public void onInit(int status) {
                if(status != TextToSpeech.ERROR)
                {
                talker.setLanguage(Locale.US);
                }

            }
        });
        Button talk = (Button)this.findViewById(R.id.talk);
        talk.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                speakOut();
            }

        });


    public void speakOut()

    {
        String toSpeak = myTextbox.getText().toString();
        Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
        String full = ("My name is"+toSpeak+ "and I am having a Seizure at  38.901 Latitude and -77.031 Longitude ");
        Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
        talker.speak(full, TextToSpeech.QUEUE_FLUSH, null);
        Toast.makeText(getApplicationContext(), full, Toast.LENGTH_SHORT).show();
        talker.speak("HELLO",TextToSpeech.QUEUE_FLUSH, null);
    }


}

Upvotes: 0

Views: 129

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

There are several problems with your code. You should check for error returned by setLanguage. You cannot call speakOut until onInit has been called. One way you can insure this is to disable the talk button in the xml layout file and enable it in onInit.

Upvotes: 1

Related Questions