Text To Speech在Ginger Bread中无效 - Android 2.3

时间:2013-07-01 09:32:55

标签: android

我正在开发一个文本到语音应用程序。 所有代码在Android 4.0冰淇淋三明治及以上版本均可正常使用。 但在Android 2.3姜面包没有任何声音没有任何错误。 语音进入模拟器而不是电话。 我已经在两部手机上查看了我的应用程序:

  1. Xperia x10和
  2. 有姜面包的Galaxy pop mini。
  3. 这是我的代码:

    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!");
            }
        }
    
    
    
    }
    

    请帮忙。

1 个答案:

答案 0 :(得分:1)

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

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 + ")");
    }
}

最初发布here