5 Replies - 1044 Views - Last Post: 06 January 2012 - 08:17 PM

#1 Talcik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

What am I doing wrong? TextToSpeech

Posted 05 January 2012 - 02:33 AM

ok basically I have my entire code for text to speech here, but when I press the button it just does not want to speak at all? and I am extremely confused I thought I have everything set up correctly to work but it is still not working

package com.write.it;

import java.util.HashMap;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Speech extends Activity implements OnInitListener, OnUtteranceCompletedListener {
	private EditText words = null;
	private Button speakBtn = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
	private static final String TAG = "TTS Demo";
    private TextToSpeech mTts;

    private int uttCount = 0;
    private HashMap<String, String> params = new HashMap<String, String>();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        words = (EditText)findViewById(R.id.wordsToSpeak);
        speakBtn = (Button)findViewById(R.id.speak);
        


        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    

    
	public void doSpeak(View view) {
		StringTokenizer st = new StringTokenizer(words.getText().toString(),",.");
		while (st.hasMoreTokens()) {
    		params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                    String.valueOf(uttCount++));
			mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);
	     }
		speakBtn.setonclickListener(new View.onclickListener() {
			public void onclick(View v) {
				// /TODO Auto-generated method stub
				doSpeak(v);
		
	}
});
		
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                // TTS is up and running
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                // missing data, install it
                Log.v(TAG, "Need language stuff: " + resultCode);
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS not available");
            }
        }
        else {
        	// Got something else
        }
    }

	public void onInit(int status) {
		// Now that the TTS engine is ready, we enable the button
		if( status == TextToSpeech.SUCCESS) {
			mTts.setOnUtteranceCompletedListener(this);
		}
	}

	@Override
	public void onpause()
	{
		super.onpause();
		// if we're losing focus, stop talking
		if( mTts != null)
			mTts.stop();
	}

	@Override
    public void onDestroy()
	{
        super.onDestroy();
        mTts.shutdown();
    }

	public void onUtteranceCompleted(String uttId) {
		Log.v(TAG, "Got completed message for uttId: " + uttId);
		Integer.parseInt(uttId);
	}


}



Is This A Good Question/Topic? 0
  • +

Replies To: What am I doing wrong? TextToSpeech

#2 Apokio  Icon User is online

  • D.I.C Addict

Reputation: 132
  • Posts: 501
  • Joined: 14-August 09

Re: What am I doing wrong? TextToSpeech

Posted 05 January 2012 - 12:59 PM

Just from initially looking at it and not testing anything. In your doSpeak method you set the listener in the method but then the button listener calls the doSpeak method. You need to set the listener in the on create method. I always set my listeners like:

speakBtn.setonclickListener(this);


In the onCreate method. Then if you are using eclispe it will set up the other methods you need for that listener. Then set it call the doSpeak method in that listener and it should work if the rest of the code is ok.

public void onclick(View v) {  
// /TODO Auto-generated method stub
  switch(v.getSource()) {
     case speakBtn:
       doSpeak();
       break;  
  }




I think that code is right for the case statement. (I am at work and don't have any Android stuff here at work)

Also I don't think the doSpeak method needs the View parameter if you change the way the listener is set.

Basically you are never setting the listener because the code is never getting to that point. You are LogCat to verify other things use it to verify that the listener is being set on the button press.

This post has been edited by Apokio: 05 January 2012 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 Talcik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: What am I doing wrong? TextToSpeech

Posted 05 January 2012 - 10:06 PM

Ok im setting it up just like you said and it seems to be ok now I just have to try it out
Was This Post Helpful? 0
  • +
  • -

#4 Talcik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: What am I doing wrong? TextToSpeech

Posted 05 January 2012 - 10:35 PM

View PostTalcik, on 05 January 2012 - 10:06 PM, said:

Ok im setting it up just like you said and it seems to be ok now I just have to try it out


no Actually it didnt want to work it says the v.getsource isnt avaliable and the onclick method isnt right? so ill have to look at it again, and go over it
Was This Post Helpful? 0
  • +
  • -

#5 Apokio  Icon User is online

  • D.I.C Addict

Reputation: 132
  • Posts: 501
  • Joined: 14-August 09

Re: What am I doing wrong? TextToSpeech

Posted 06 January 2012 - 06:52 AM

I edited your code here. It fixes the problem of it not getting to speak button and doSpeak() method. I still don't think it is working correctly but i think your problem my be elsewhere that is over my head with the text to speech stuff.

package com.write.it;

import java.util.HashMap;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;

public class Speech extends Activity implements OnInitListener, OnUtteranceCompletedListener, onclickListener {
	private EditText words = null;
	private Button speakBtn = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
	private static final String TAG = "TTS Demo";
    private TextToSpeech mTts;

    private int uttCount = 0;
    private HashMap<String, String> params = new HashMap<String, String>();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        words = (EditText)findViewById(R.id.wordsToSpeak);
        speakBtn = (Button)findViewById(R.id.speak);
        speakBtn.setonclickListener(this);
        


        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    

    
	public void doSpeak() {
		StringTokenizer st = new StringTokenizer(words.getText().toString(),",.");
		while (st.hasMoreTokens()) {
    		params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                    String.valueOf(uttCount++));
			mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);
	     }
		//speakBtn.setonclickListener(new View.onclickListener() {
			//public void onclick(View v) {
				// /TODO Auto-generated method stub
				//doSpeak(v);
		
	//}
		
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                // TTS is up and running
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                // missing data, install it
                Log.v(TAG, "Need language stuff: " + resultCode);
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS not available");
            }
        }
        else {
        	// Got something else
        }
    }

	public void onInit(int status) {
		// Now that the TTS engine is ready, we enable the button
		if( status == TextToSpeech.SUCCESS) {
			mTts.setOnUtteranceCompletedListener(this);
		}
	}

	public void onpause()
	{
		super.onpause();
		// if we're losing focus, stop talking
		if( mTts != null)
			mTts.stop();
	}

	@Override
    public void onDestroy()
	{
        super.onDestroy();
        mTts.shutdown();
    }

	public void onUtteranceCompleted(String uttId) {
		Log.v(TAG, "Got completed message for uttId: " + uttId);
		Integer.parseInt(uttId);
	}



	@Override
	public void onclick(View v) {
		switch(v.getId()){
		case R.id.speak:
			doSpeak();
			break;
		}
		
	}


}




The ode tags sometimes mess with the capitalization of some of the methods. onclick should be onclick are you using something like eclipse to do this in? Try using the debugger to step through your code and see exactly what it is doing.

This post has been edited by Apokio: 06 January 2012 - 06:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 Talcik  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 13-November 11

Re: What am I doing wrong? TextToSpeech

Posted 06 January 2012 - 08:17 PM

View PostApokio, on 06 January 2012 - 06:52 AM, said:

I edited your code here. It fixes the problem of it not getting to speak button and doSpeak() method. I still don't think it is working correctly but i think your problem my be elsewhere that is over my head with the text to speech stuff.

package com.write.it;

import java.util.HashMap;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;

public class Speech extends Activity implements OnInitListener, OnUtteranceCompletedListener, onclickListener {
	private EditText words = null;
	private Button speakBtn = null;
    private static final int REQ_TTS_STATUS_CHECK = 0;
	private static final String TAG = "TTS Demo";
    private TextToSpeech mTts;

    private int uttCount = 0;
    private HashMap<String, String> params = new HashMap<String, String>();

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        words = (EditText)findViewById(R.id.wordsToSpeak);
        speakBtn = (Button)findViewById(R.id.speak);
        speakBtn.setonclickListener(this);
        


        // Check to be sure that TTS exists and is okay to use
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, REQ_TTS_STATUS_CHECK);
    }
    

    
	public void doSpeak() {
		StringTokenizer st = new StringTokenizer(words.getText().toString(),",.");
		while (st.hasMoreTokens()) {
    		params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
                    String.valueOf(uttCount++));
			mTts.speak(st.nextToken(), TextToSpeech.QUEUE_ADD, params);
	     }
		//speakBtn.setonclickListener(new View.onclickListener() {
			//public void onclick(View v) {
				// /TODO Auto-generated method stub
				//doSpeak(v);
		
	//}
		
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQ_TTS_STATUS_CHECK) {
            switch (resultCode) {
            case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
                // TTS is up and running
                mTts = new TextToSpeech(this, this);
                Log.v(TAG, "Pico is installed okay");
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_BAD_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_DATA:
            case TextToSpeech.Engine.CHECK_VOICE_DATA_MISSING_VOLUME:
                // missing data, install it
                Log.v(TAG, "Need language stuff: " + resultCode);
                Intent installIntent = new Intent();
                installIntent.setAction(
                    TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
                break;
            case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
            default:
                Log.e(TAG, "Got a failure. TTS not available");
            }
        }
        else {
        	// Got something else
        }
    }

	public void onInit(int status) {
		// Now that the TTS engine is ready, we enable the button
		if( status == TextToSpeech.SUCCESS) {
			mTts.setOnUtteranceCompletedListener(this);
		}
	}

	public void onpause()
	{
		super.onpause();
		// if we're losing focus, stop talking
		if( mTts != null)
			mTts.stop();
	}

	@Override
    public void onDestroy()
	{
        super.onDestroy();
        mTts.shutdown();
    }

	public void onUtteranceCompleted(String uttId) {
		Log.v(TAG, "Got completed message for uttId: " + uttId);
		Integer.parseInt(uttId);
	}



	@Override
	public void onclick(View v) {
		switch(v.getId()){
		case R.id.speak:
			doSpeak();
			break;
		}
		
	}


}




The ode tags sometimes mess with the capitalization of some of the methods. onclick should be onclick are you using something like eclipse to do this in? Try using the debugger to step through your code and see exactly what it is doing.



Yeah man basically it is telling me that Speech is unimplemented and I have to add a method to it, causing me to have to onclick methods so therefore its a pain in my butt, but thank you for my help ill have to figure this out, ill try the debugger
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1