AVSpeechUtterance作为UILocalNotification声音

时间:2014-05-07 14:27:21

标签: ios ios7 background uilocalnotification avspeechsynthesizer

您好,感谢您阅读本文!

在前台,我的应用为定时事件播放iOS7 AVSpeechUtterance。在后台,我将.wav文件名传递给soundName属性UILocalNotification。一切正常。

有没有办法将AVSpeechUtterance用作UILocalnotification的声音?我想在前景中使用与背景相同的合成。

非常感谢!

1 个答案:

答案 0 :(得分:0)

appdelegate.m文件中创建此方法。 不要忘记在Appdelegate.h中导入AVFoundation / AVSpeechSynthesizer.h

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    AVSpeechUtterance *utterance = [AVSpeechUtterance
                                    speechUtteranceWithString:[NSString stringWithFormat:@"%@",notification.alertBody]];
    AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
    utterance.volume = 10;
    utterance.rate = 0.2;
    utterance.pitchMultiplier = 1;
    [synth speakUtterance:utterance];

    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}