0

I am trying to make a read aloud menu option here. This is the code I have written. But it doesn't really read aloud the text on the card. The doc says that if there is any text set on the card it will read aloud. My card has some text on it :

newcard.setText("text");

My MenuActivity which is called looks like this.

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();
    }
}

I am really not sure wheteher I can pass the JSON like this. If not like this, how else can I do this?

twlkyao
  • 14,302
  • 7
  • 27
  • 44
Chirag
  • 335
  • 2
  • 3
  • 13

1 Answers1

0

I am not sure where you read that Glass would automatically read this aloud...

You should use TextToSpeech as in the gdk-compass-sample provided by Google.

Create a field in your Activity

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

Initialise in OnCreate

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

Call the speak function in your onOptionsItemSelected event

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

Close everything down in the onDestroy event

@Override
public void onDestroy() {
    mSpeech.shutdown();
    mSpeech = null;
    super.onDestroy();
}
Ferdau
  • 1,524
  • 1
  • 12
  • 21
  • Sorry for being a noob, but what is text in mSpeech.speak(json(item.getItemId()).text, TextToSpeech.QUEUE_FLUSH, null); Its giving an error so its not the text in obj.put("text", "Hello World"); – Chirag Feb 25 '14 at 11:06
  • What error does it throw? Try: mSpeech.speak("hello", TextToSpeech.QUEUE_FLUSH, null); If this works check why you don't get the right text... If you want to know what it exactly does, first go dive into the documentations, if you don't get it, ask your question here. – Ferdau Feb 26 '14 at 10:26
  • Okay, i tried as suggested, it doesn't even read the "hello". I will surely go through the documenatation for further insight. – Chirag Feb 26 '14 at 12:59