可可:言语和时间

发布于 2024-12-11 17:53:24 字数 242 浏览 1 评论 0原文

我正在构建一个应用程序,其中一部分会说出时间。但是,当我将日期字符串(如 10/24/11)传递给 NSSpeechSynthesizer 时,它会按字面意思发音,如“一、零、斜线二四斜线一一”,与时间戳相同,“八冒号一一”冒号冒号”等。

我查看了 NSSpeechSynthesizer 文档,我想我必须使用phonemesFromText 方法,但这似乎需要大量繁重的工作才能获得该应用程序顺利说出时间和日期。有没有更快的方法?

谢谢

I'm building an app that a part of will speak the time. However, when I pass in my date strings (like 10/24/11) to the NSSpeechSynthesizer it will speak them literally, as "one, zero, slash two four slash one one", same with a timestamp, "eight colon one one colon colon", etc. etc.

I looked at the NSSpeechSynthesizer docs and I guess I'd have to work with the phonemesFromText method but that seems like a lot of grunt work to get the app to speak the time and date smoothly. Is there a quicker method?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-12-18 17:53:24

您可以尝试这样的操作:

@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:一个将日期的字符串表示形式“翻译”为实际的 NSDate 对象,然后另一个将 NSDate 转换回更理想的字符串表示形式。

显然,您需要调整 dateParser 格式以适合您预期的输入字符串类型。 (最好,您可以只使用输入日期而不是它的字符串表示形式)。

You could try something like this:

@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

Basically, this is using 2 NSDateFormatters: one to "translate" the string representation of a date into an actual NSDate object, and then another to translate that NSDate back into a more desirable string representation.

Obviously, you'll need to adjust the dateParser format to fit your expected input string type. (Preferably, you could just use an input date rather than the string representation of it).

瞎闹 2024-12-18 17:53:24

Why not use NSDateComponents to and -[NSString stringWithFormat:] construct a spoken sentence as your string, then speak that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文