Cocoa 可可:演讲与时间
我正在构建一个应用程序,其中一部分将随时发言。然而,当我将日期字符串(如10/24/11)传递给NSSpeechSynthesizer时,它会逐字说出它们,如“1、0、斜杠2、4、斜杠1、1”,与时间戳相同,“8冒号1、1冒号”等 我查看了NSSpeechSynthesizer文档,我想我必须使用phonemesFromText方法,但要让应用程序流畅地说出时间和日期,这似乎需要做很多繁重的工作。有没有更快的方法Cocoa 可可:演讲与时间,cocoa,time,speech,Cocoa,Time,Speech,我正在构建一个应用程序,其中一部分将随时发言。然而,当我将日期字符串(如10/24/11)传递给NSSpeechSynthesizer时,它会逐字说出它们,如“1、0、斜杠2、4、斜杠1、1”,与时间戳相同,“8冒号1、1冒号”等 我查看了NSSpeechSynthesizer文档,我想我必须使用phonemesFromText方法,但要让应用程序流畅地说出时间和日期,这似乎需要做很多繁重的工作。有没有更快的方法 谢谢为什么不使用to和-[NSString stringWithFormat:]构
谢谢为什么不使用to和-[NSString stringWithFormat:]构造一个口语句子作为您的字符串,然后说出来?您可以尝试以下方法:
@implementation MDAppController
- (id)init {
if ((self = [super init])) {
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *dateParser = [[[NSDateFormatter alloc]
initWithDateFormat:@"%m/%d/%y" allowNaturalLanguage:YES] autorelease];
NSDate *date = [dateParser dateFromString:@"10/24/11"];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *string = [dateFormatter stringFromDate:date];
NSLog(@"string == %@", string);
// prints "October 24, 2011"
NSSpeechSynthesizer *alex = [[NSSpeechSynthesizer alloc]
initWithVoice:[NSSpeechSynthesizer defaultVoice]];
[alex setDelegate:self];
[alex startSpeakingString:string];
}
- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender
didFinishSpeaking:(BOOL)finishedSpeaking {
if (finishedSpeaking) [sender autorelease];
}
@end
基本上,这是使用2个NSDateFormatter
s:一个将日期的字符串表示形式“转换”为实际的NSDate
对象,然后另一个将该NSDate
转换回更理想的字符串表示形式
显然,您需要调整
dateParser
格式以适合预期的输入字符串类型。(最好只使用输入日期,而不是它的字符串表示形式)。您可以将自己的日期滚动到可说出的文本函数,以便将日期10/24/11转换为“2011年10月10日”或类似的内容。谢谢,看起来我将滚动我自己的日期。您仍然需要从10-->“10日”开始,2011年-->“二千零一一”或“二一一”或任何不正确的。这是一个与此不同的问题(即使相关)。这里有一个很好的答案:谢谢Joshua,这是一个很好的提示,也不知道NSDateComponents,很好的东西。