atom feed5 messages in NSSpeechRecognizer
FromSent OnAttachments
Reema SardanaDec 9, 2008 12:00 am 
Andrew ThompsonDec 9, 2008 7:17 pm 
Reema SardanaDec 19, 2008 9:11 am 
Andrew ThompsonDec 28, 2008 11:48 am 
Reema SardanaDec 28, 2008 12:32 pm 
Subject:Re: NSSpeechRecognizer
From:Andrew Thompson (
Date:Dec 28, 2008 11:48:58 am


It appears you have a threading rules problem. As you may know, in Java some methods must be called from the AWT/Swing event dispatch thread. Cocoa has similar rules... it has a concept of a Main Thread and many NSXXX classes must be used from the main thread. Just as in Java, sometimes you get away with it even if you break this rule, and sometimes you don't.

Acting on a hunch, after a couple of hours of messing around, I was able to get your code to work, simply by adding:

import org.rococoa.RunOnMainThread

@RunOnMainThread public abstract class NSSpeechRecognizer implements NSObject

Now I can say "hello" etc and see it printed in the Java console as you intend. The solution was simple, but it took me a while to think of it.

On Dec 19, 2008, at 12:11 PM, Reema Sardana wrote:

Thanks for the response Andrew,

I implemented the NSSpeechSynthesizer too and that works perfectly for me. However, I did not have to use any delegate methods in that class. However, the NSSpeechRecognizer class does not seem to be recognizing any commands for me. After what you suggested, this is what my code looks like:

public abstract class NSSpeechRecognizer implements NSObject { public static final _Class CLASS = Rococoa.createClass("NSSpeechRecognizer", _Class.class); private NSObject delegateWrapper; private NSSpeechRecognizerDelegate delegate;

public static abstract class _Class implements NSClass { public _Class() {} public abstract NSSpeechRecognizer alloc(); }

public interface NSSpeechRecognizerDelegate { public void speechRecognizer_didRecognizeCommand(NSSpeechRecognizer sender, ID command); }

public synchronized void setDelegate(final NSSpeechRecognizerDelegate delegate) { this.delegate = delegate; delegateWrapper = Rococoa.proxy(delegate); setDelegate(; }

public synchronized NSSpeechRecognizerDelegate getDelegate() { return delegate; }

abstract NSArray commands(); abstract void setCommands(NSArray commands); abstract ID delegate(); abstract void setDelegate(ID delegate); abstract void setDisplayedCommandsTitle(String title); abstract void setListensInForegroundOnly(boolean flag); abstract void setBlocksOtherRecognizers(boolean flag); abstract void startListening(); abstract void stopListening(); abstract NSSpeechRecognizer init(); }

**************************************************************************************************** Delegate:

public class HearsayDelegate implements NSSpeechRecognizerDelegate { public void speechRecognizer_didRecognizeCommand(NSSpeechRecognizer sender, ID command) { System.out.println("Hello: " + command); } }


Main class:


NSSpeechRecognizer recog = NSSpeechRecognizer.CLASS.alloc().init(); recog.setDelegate(new HearsayDelegate());

NSArray commands = NSArray.CLASS.arrayWithObjects( NSString.stringWithString("Hello"), NSString.stringWithString("Welcome"), NSString.stringWithString("One") );

recog.setCommands(commands); recog.setListensInForegroundOnly(false); recog.startListening();


AndyT (lordpixel - the cat who walks through walls) A little bigger on the inside

(see you later space cowboy, you can't take the sky from me)