views:

18

answers:

0

I am implementing Text to Speech for Iphone, and successfully implemented it, but how to retain value in text field when second time I re-open my application. I want to read the text from where I left. So is NSUserdefault is good approach?

But what if when i using a file instead of textbox and want to start reading from same place where I left.

I am enclosing my sample code as below.

-(void)speakText:(NSString *)text
{
NSMutableString *cleanString;
cleanString = [NSMutableString stringWithString:@""];
if([text length] > 1)
{
    int x = 0;
    while (x < [text length])
    {
        unichar ch = [text characterAtIndex:x];
        [cleanString appendFormat:@"%c", ch];
        x++;
    }
}
if(cleanString == nil)
{   // string is empty
    cleanString = [NSMutableString stringWithString:@""];
}
sound = flite_text_to_wave([cleanString UTF8String], voice);


NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recordingDirectory = [filePaths objectAtIndex: 0];
// Pick a file name
NSString *tempFilePath = [NSString stringWithFormat: @"%@/%s", recordingDirectory, "temp.wav"];
// save wave to disk
char *path; 
path = (char*)[tempFilePath UTF8String];
cst_wave_save_riff(sound, path);
// Play the sound back.
NSError *err;
[audioPlayer stop];
audioPlayer =  [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:tempFilePath] error:&err];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer play];
// Remove file
[[NSFileManager defaultManager] removeItemAtPath:tempFilePath error:nil];

}