1

我正在开发一个文本到语音应用程序。所有代码在 Android 4.0 冰淇淋三明治及更高版本中运行良好。但是在 Android 2.3 姜饼中,没有任何声音没有任何错误。语音进入模拟器,但没有在电话中。我在两部手机上检查了我的应用程序:

  1. Xperia x10 和
  2. Galaxy pop mini 有姜饼。

这是我的代码:

import java.util.Locale;
import android.view.ViewGroup;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TTS extends Activity implements OnInitListener{

    Context context;
    private TextToSpeech tts;
    private ImageButton playTTS,stopTTS,clearTTS;
    private EditText textTTS;
    private Spinner selectVoices;
    public static int spinnerPos;
    private String spinnerList[] = {"...Select Voices..."," Voice 1"," Voice 2"," Voice 3"};

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

        tts = new TextToSpeech(this, this);

        selectVoices = (Spinner) findViewById(R.id.spinner);

        playTTS = (ImageButton) findViewById(R.id.b_playTTS);
        stopTTS = (ImageButton) findViewById(R.id.b_stopTTS);
        clearTTS = (ImageButton) findViewById(R.id.b_clearTTS);

        textTTS = (EditText) findViewById(R.id.et_entertext);

        Typeface  mFace = Typeface.createFromAsset(this.getAssets(),"mvboli.ttf");
        textTTS.setTypeface(mFace);

        ArrayAdapter<String> aa = new ArrayAdapter<String>(TTS.this, R.layout.spinnerlayout, spinnerList){

            public View getView(int position, View convertView, ViewGroup parent) {
                    View v = super.getView(position, convertView, parent);

                    Typeface externalFont=Typeface.createFromAsset(getAssets(), "mvboli.ttf");
                    ((TextView) v).setTypeface(externalFont);

                    return v;
            }


            public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
                     View v =super.getDropDownView(position, convertView, parent);

                    Typeface externalFont=Typeface.createFromAsset(getAssets(), "mvboli.ttf");
                    ((TextView) v).setTypeface(externalFont);

                    return v;
            }
    };
        selectVoices.setAdapter(aa);

        // spinner on item click event
        selectVoices.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                spinnerPos = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        // button on click event
        playTTS.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                switch (spinnerPos) {
                case 0:
                    Toast.makeText(getBaseContext(), "please choose a voice type !", Toast.LENGTH_SHORT).show();
                    break;

                case 1:
                    tts.setPitch(1.0f);
                    speakOut();
                    Log.d("TextToSpeech","in the onclick1");
                    break;

                case 2:
                    tts.setPitch(0.2f);
                    speakOut();
                    Log.d("TextToSpeech","in the onclick2");
                    break;

                case 3:
                    //pitch = 0.2f;
                    tts.setPitch(2.0f);
                    speakOut();
                    Log.d("TextToSpeech","in the onclick3");
                    break;

                default:
                    break;
                }
            }

        });

        // clear button click event
        stopTTS.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tts.stop();

            }
        });

        // back button click event
        clearTTS.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                textTTS.setText("");            }
        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }


    private void speakOut() {

        String text = textTTS.getText().toString();
        Log.d("TextToSpeech","in the spkout");

        tts.speak(text,tts.QUEUE_FLUSH,null);
    }

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.getDefault());

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } 
            else{
                if (tts.isLanguageAvailable(Locale.getDefault()) == TextToSpeech.LANG_AVAILABLE || tts.isLanguageAvailable(Locale.getDefault()) == TextToSpeech.LANG_COUNTRY_AVAILABLE)
                    tts.setLanguage(Locale.getDefault());
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }



}

请帮忙。

4

1 回答 1

2

是的,有一种方法,但不是未经用户同意。

private void installVoiceData() {
    Intent intent = new Intent(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.google.android.tts"/*replace with the package name of the target TTS engine*/);
    try {
        Log.v(TAG, "Installing voice data: " + intent.toUri(0));
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        Log.e(TAG, "Failed to install TTS data, no acitivty found for " + intent + ")");
    }
}

最初发布在这里

于 2013-07-03T10:15:29.107 回答