Monday, August 16, 2010

Text to speech using VSSpeechSynthesizer

The VSSpeechSynthesizer is the text to speech API in the private VoiceServices framework for IPhone OS 4.0. Appearently, it only available for IPhone 3GS or above. As the VoiceServices is not a public framework, application that is dependent on it may not be allow to publish to Apple appstore.

It is quite a powerful API for text to speech, however it is not very well documented as it is in a private framework. To test out the API follow the following steps:

1. Create the VSSpeechSynthesizer.h file and add it into the project.

Code:
#import  <foundation/foundation.h>

@interface VSSpeechSynthesizer : NSObject
{
}

+ (id)availableLanguageCodes;
+ (BOOL)isSystemSpeaking;
- (id)startSpeakingString:(id)string;
- (id)startSpeakingString:(id)string toURL:(id)url;
- (id)startSpeakingString:(id)string toURL:(id)url withLanguageCode:(id)code;
- (float)rate;             // default rate: 1
- (id)setRate:(float)rate;
- (float)pitch;           // default pitch: 0.5
- (id)setPitch:(float)pitch;
- (float)volume;       // default volume: 0.8
- (id)setVolume:(float)volume;
@end

2. Create and initiate the VSSpeechSynthesizer object in the appropiate area of the code.

Code:
VSSpeechSynthesizer *speech = [[NSClassFromString(@"VSSpeechSynthesizer") alloc] init];

3. Adjust the pitch, rate or volume accordingly.

Eg:
[speech setRate:(float)1.0];

4. Create the speech in the appropiate area of the code.

Eg:
[speech startSpeakingString:@"Hello world, how are you"];

5. Include the private VoiceServices into the frameworks

Eg:

Depending on where you install the IPhone SDK, the location of the VoiceServices framework may varies. Usually the VoiceServices framework is in the following directory:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.0.sdk
/System/Library/PrivateFrameworks/VoiceServices.framework/VoiceServices

*Note: The VoiceServies framework only available in the iPhoneOS platform, hence it can only build and run on the actual iPhone device. It can not be run and test on the simulator.







Sample code:

Friday, August 06, 2010

Auto show keyboard

To automatically show the keyboard in any view for a specific UITextField, just set the UITextField as the first responder in the viewDidLoad method.

Eg:
     - (void)viewDidLoad {

               [super viewDidLoad];
              [commentTxt becomeFirstResponder];


    }

Thursday, August 05, 2010

Programming IPhone to support rotation auto orientation change

Everytime when IPhone is rotated the shouldAutorotateToInterfaceOrientation method is called.
To support this, just return YES.

Eg:
       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

                  // Return YES for supported orientations
                 return YES;
      }