2014-02-24 57 views
0

我想在這裏做一個朗讀菜單選項。這是我寫的代碼。但它並不真正朗讀卡片上的文字。該文件說,如果卡上有任何文本,它會大聲朗讀。我的卡上有一些文字:菜單選項朗讀不起作用

newcard.setText("text");

MenuActivity這就叫這個樣子的。

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

public class MenuActivity extends Activity { 

    public Object json(int menuid) 
    { 
     JSONObject obj = new JSONObject(); 
     try { 
      System.out.println("goes into try of json"); 
      obj.put("text", "Hello World"); 
      obj.put("menuItems", new JSONObject().put("action", "READ_ALOUD")); 
      System.out.println(obj); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return obj; 
    } 

    @Override 
    public void onAttachedToWindow() { 
     super.onAttachedToWindow(); 
     openOptionsMenu(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.second, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection. 
     switch (item.getItemId()) { 
      case R.id.read_aloud_menu_item: 
       System.out.println("goes itno read aloud case"); 
       json(item.getItemId()); 

       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    @Override 
    public void onOptionsMenuClosed(Menu menu) { 
     // Nothing else to do, closing the activity. 
     finish(); 
    } 
} 

我真的不確定是否可以通過這樣的JSON。如果不是這樣,我該怎麼辦?

回答

0

我不知道你在哪裏讀到,玻璃會自動讀取這個大聲...

您應該使用TextToSpeech爲谷歌提供的gdk-compass-sample

Activity

public class MenuActivity extends Activity { 
    private TextToSpeech mSpeech; 
    ... 
} 

初始化在OnCreate

@Override 
public void onCreate() { 
    super.onCreate(); 
    ... 
    mSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      // Do nothing. 
     } 
    }); 
} 

調用speak功能在您的onOptionsItemSelected事件創建一個域

mSpeech.speak(json(item.getItemId()).text, TextToSpeech.QUEUE_FLUSH, null); 

關閉一切都在onDestroy下降事件

@Override 
public void onDestroy() { 
    mSpeech.shutdown(); 
    mSpeech = null; 
    super.onDestroy(); 
} 
+0

對不起,我是一個菜鳥,但什麼是 mSpeech.speak文本(JSON(item.getItemId())的文本,TextToSpeech.QUEUE_FLUSH,空); 它給出一個錯誤,所以它不是在obj.put文本(「文本」,「你好世界」); – Chirag

+0

它拋出了什麼錯誤?試試:mSpeech.speak(「hello」,TextToSpeech.QUEUE_FLUSH,null);如果這能起作用,請檢查你爲什麼沒有找到正確的文本......如果你想知道它到底是什麼,那麼首先要深入文檔,如果你沒有得到它,請在這裏提出你的問題。 – Ferdau

+0

好的,我按照建議試過,它甚至沒有讀「你好」。我一定會通過文件進一步瞭解。 – Chirag