2013-07-01 31 views
0

我正在開發一個文本到語音應用程序。 所有代碼在Android 4.0冰淇淋三明治及以上版本都正常工作。 但是在Android 2.3姜麪包裏沒有任何聲音沒有任何錯誤。 聲音進入模擬器,但不在電話中。 我已經檢查了兩款手機我的應用程序:文本到語音不能在姜麪包工作 - Android 2.3

  1. Xperia X10的,並具有薑餅
  2. 銀河流行的迷你。

這裏是我的代碼:

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



} 

請幫助。

+0

您可能沒有安裝默認的語音引擎。檢查文本到語音設置並說安裝語音數據。 – Aswin

+0

感謝名單阿什溫...但有什麼辦法,當有人安裝我的應用程序,然後默認引擎會自動安裝或者我怎麼給我的語音引擎,以我的應用程序... ??? – cyborg

+0

是的,有一種方法。檢查下面的答案。 – Aswin

回答

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