2013-05-31 20 views
2

我對主人的問候。我目前正在開發類似於「待辦事項列表」的應用程序。我已成功實施獲取Notifications。我試圖實現文本到語音,使我的Sony Xperia Tipo Dual ST21i2在預先規定的時間講出我添加的任務。但是我從手機裏聽不到任何東西。文本到語音在Android中不工作

public class NotifyService extends Service implements OnInitListener{ 

    private TextToSpeech tts; 
    int task_id; 
    private static final int NOTIFICATION = 123; 
    public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY"; 
    private NotificationManager mNM; 
    SQLiteDatabase database; 
    String tmp_task_brief = null; 

    public class ServiceBinder extends Binder 
    { 
     NotifyService getService() 
     { 
      return NotifyService.this; 
     } 
    } 

    @Override 
    public void onCreate() { 

     mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     tts = new TextToSpeech(getApplicationContext(), this); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     task_id = intent.getIntExtra("task_id", 0); 

     loadDatabase(); 
     Cursor cursor = database.query("task_info", new String[]{"task_brief"}, "task_id=?", new String[]{task_id+""}, null, null, null); 
     while(cursor.moveToNext()) 
     { 
      tmp_task_brief = cursor.getString(0); 
     } 
     cursor.close(); 
     database.close(); 

     if(intent.getBooleanExtra(INTENT_NOTIFY, false)) 
      showNotification(tmp_task_brief); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     return mBinder; 
    } 

    private final IBinder mBinder = new ServiceBinder(); 

    private void showNotification(String tmp_task_brief) { 

     CharSequence title = "To Do Task Notification!!"; 
     int icon = R.drawable.ic_menu_notifications; 
     CharSequence text = tmp_task_brief;  
     long time = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, text, time); 

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0); 

     notification.setLatestEventInfo(this, title, text, contentIntent); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     mNM.notify(NOTIFICATION, notification); 

     //---------vibrate on notification----------- 

     Vibrator vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

     int dot = 200; 
     int dash = 500; 
     int short_gap = 200; 
     int medium_gap = 500; 
     int long_gap = 1000; 
     long[] pattern = { 
      0, // Start immediately 
      dot, short_gap, dot, short_gap, dot, // s 
      medium_gap, 
      dash, short_gap, dash, short_gap, dash, // o 
      medium_gap, 
      dot, short_gap, dot, short_gap, dot, // s 
      long_gap 
     }; 

     // Only perform this pattern one time (-1 means "do not repeat") 
     vibrate.vibrate(pattern, -1); 

     speakOut(tmp_task_brief); 

     stopSelf(); 
    } 

    void loadDatabase() 
    { 
     database = openOrCreateDatabase("ToDoDatabase.db", 
       SQLiteDatabase.OPEN_READWRITE, null); 
    } 

    @Override 
    public void onInit(int status) { 
     // TODO Auto-generated method stub 

     if (status == TextToSpeech.SUCCESS) { 

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

      if (result == TextToSpeech.LANG_MISSING_DATA 
        || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
       Log.i("TTS", "This Language is not supported"); 
       Toast.makeText(getApplicationContext(), "This Language is not supported", Toast.LENGTH_SHORT).show(); 
      } else { 
       speakOut(tmp_task_brief); 
      } 

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

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

    private void speakOut(String task) { 

     tts.speak(task, TextToSpeech.QUEUE_FLUSH, null); 
    } 
} 

我記錄的輸出如下錯誤:

05-31 11:49:00.450: I/TextToSpeech(11621): Sucessfully bound to com.google.android.tts 
05-31 11:49:00.490: W/TextToSpeech(11621): speak failed: not bound to TTS engine 
05-31 11:49:00.490: I/TextToSpeech(11621): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.GoogleTTSService} 

請幫我解決這個錯誤。

回答

2

在speak方法有機會發言之前,請致電stopSelf()。您應該執行OnUtteranceCompletedListener並在onUtteranceCompleted之內撥打stopSelf()
此外,speakOut(tmp_task_brief);不應在showNotification方法內調用,因爲speak方法僅在調用onInit後才起作用。

+0

謝謝先生。我達到了我想要的。我想問你一件事。我在這個應用程序中實現了語音輸入工具。我是否需要數據連接,以使語音到文本起作用? –

+0

您確實需要互聯網連接才能使語音正常工作。但是,您可以使用createSoundFile創建聲音文件。之後,你不需要互聯網連接。 –

+0

聽起來不錯。請你給我提供一些例子,鏈接或教程。請。 –

0

我在處理文本到語音時遇到了同樣的問題。當您在設備上禁用Google文本到語音轉換時會發生這種情況。 嘗試更新Google文字轉語音或啓用它。