NSSpeechSynthesizer not calling delegate methods

Discussion in 'Mac Programming' started by drskot, Nov 9, 2011.

  1. drskot, Nov 9, 2011
    Last edited: Nov 9, 2011

    drskot macrumors newbie

    Joined:
    Jul 11, 2010
    #1
    Has anybody had any luck using an NSSpeechSynthesizerDelegate? None of my delegate's methods are being called.

    Here's the code:


    Code:
    
    #import <Foundation/Foundation.h>
    #import <AppKit/AppKit.h>
    
    @interface SynthesizerDelegate : NSObject <NSSpeechSynthesizerDelegate>
    @end
    
    @implementation SynthesizerDelegate
    - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender willSpeakWord:(NSRange)characterRange ofString:(NSString *)string {
      printf("speaking word ");
      printf("%s \n", [[string substringWithRange:characterRange] UTF8String]);
    }
    - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking {
      printf("finished speaking\n");
    }
    @end
    
    int main (int argc, const char * argv[])
    {
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
      
      NSString *input = @"I am not an animal. I am a man.";
      
      NSSpeechSynthesizer *synthesizer = [[NSSpeechSynthesizer alloc] init];
      SynthesizerDelegate *delegate = [[SynthesizerDelegate alloc] init];
      [synthesizer setDelegate:delegate];
      
      [synthesizer startSpeakingString:input];
      
      while ([synthesizer isSpeaking]) {
        sleep(1);
      }
      
      [synthesizer release];
      [delegate release];
      
      [pool drain];
      return 0;
    }
    
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You need an NSRunLoop for the delegates to work.
     
  3. drskot thread starter macrumors newbie

    Joined:
    Jul 11, 2010

Share This Page