开发者

Failure in resolving TextToSpeech class for Android Development

I'm new to Android development, Eclipse and Java (done mostly .Net and IVR programming up till now), so when I tried to compile and run a sample app I found for TTS on the Droid, I wasn't surprised that I got a runtime error right away. The error is:

dalvikvm Failed resolving com/sampl开发者_JAVA技巧e/TTSapp/AndroidTTSapp; interface 4 'android/speech/tts/TextToSpeech$OnInitListner;'

I suppose the OnInitListner method must be in one of the classes that was installed when I installed the Android SDK (release 1.6 R1 I believe), but I'm not sure how to import the associated class module into the current program. I can't find a speech/tts/TextToSpeech directory anywhere on my system. Do I need to download this directory from somewhere? Following is the Java source code for the demo TTS program I'm trying to run:


package com.sample.TTSApp;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.sample.TTSApp.R;
import java.util.Locale;
import java.util.Random;
public class AndroidTTSapp extends Activity implements
TextToSpeech.OnInitListener 
    {
    private static final String TAG = "TextToSpeechDemo";  
    private TextToSpeech mTts;
    private Button mAgainButton;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
        // Initialize text-to-speech. This is an asynchronous operation.  
        // The OnInitListener (second argument) is called after initialization completes.
        // Instantiate TextToSpeech.OnInitListener      
        mTts = new TextToSpeech(this, this);
        mAgainButton = (Button) findViewById(R.id.again_button);

        mAgainButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v)
            {
                sayHello();
            }
        });
    } 
    @Override
    public void onDestroy() 
    {        // Don't forget to shutdown! 
        if (mTts != null) 
            {
                mTts.stop();
                mTts.shutdown();
            }
        super.onDestroy();
    } 
    // Implements TextToSpeech.OnInitListener.

    public void onInit(int status)
    {
        // status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
        if (status == TextToSpeech.SUCCESS) 
        {       
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) 
            {
                // Language data is missing or the language is not supported.
                Log.e(TAG, "Language is not available.");  
            }
            else
            {
                // Check the documentation for other possible result codes. For example, the language may be available for the locale
                // but not for the specified country and variant.      
                // The TTS engine has been successfully initialized. Allow the user to press the button for the app to speak again. 
                mAgainButton.setEnabled(true);
                // Greet the user
                sayHello();
            }             
        } 
        else 
        {
            // Initialization failed.
            Log.e(TAG, "Could not initialize TextToSpeech.");
        }
    };

    private static final Random RANDOM = new Random();
    private static final String[] HELLOS = 
    { 
        "Hello World",  "This is Text to speech demo by Zahid Shaikh"   
    };

    private void sayHello() 
    {
        // Select a random hello.
        int i =0; 
        int helloLength = HELLOS.length;
        String hello = HELLOS[i];
        i++;
        if(i == helloLength) i =0;
        mTts.speak(hello,TextToSpeech.QUEUE_FLUSH,null);
    } 
}

Thanks in advance for any assitance anyone can give a beginner like myself.

Don Tilley


On device or emulator in */system/tts/lang_pico/* must be tts-lang-files (*.bin).

It's init TTS example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ........
    ........
    initTTS();
}

private void initTTS() {
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == MY_DATA_CHECK_CODE) {
    if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
    mTts = new TextToSpeech(this, this);
    } else {
    Intent installIntent = new Intent();
    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    startActivity(installIntent);
        }
    }
}
public void onInit(int status) {
    if(status == TextToSpeech.SUCCESS) {
        int result = mTts.setLanguage(Locale.US);
                if(result == TextToSpeech.LANG_AVAILABLE
                   || result == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
                         mTts.speak("Start system", TextToSpeech.QUEUE_FLUSH, null);
                }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜