UILocalNotification声音在循环中运行

时间:2014-07-06 13:41:49

标签: ios loops uilocalnotification

我有一个5.0秒的aiff文件。我想在循环中运行它长达30秒。请指导我该怎么做。 现在我通过以下代码以正常方式设置通知 -

//Function to schedule local notification
-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = particularfiredate;
    notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
    notification.alertBody = alarmname;
    notification.userInfo = dicttext;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

请注意,我不希望通知横幅和通知出现6次,只是声音重复最多30秒。

如果我不这样做,并添加一个30秒的声音文件,那么声音会持续到30秒,如果用户关闭其间的通知,并打开应用程序,声音会继续播放。这个问题已在此处说明:The UILocalNotification sound does not stop playing

1 个答案:

答案 0 :(得分:0)

我通过定期创建多个通知来修复它,这将是轨道的持续时间。以下是我的代码:

-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{//post one single notification here with alert body, so that there are no multiple banners for each notification.

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = particularfiredate;
notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
notification.alertBody = alarmname;
notification.userInfo = dicttext;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
totalDuration = totalDuration + [[arraySoundDuration objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]]floatValue];

while (totalDuration<30) {//while time is less than 30 secs, which is the desired time.
     NSDate *newDate1 = [particularfiredate dateByAddingTimeInterval:totalDuration];
    UILocalNotification *notification1 = [[UILocalNotification alloc] init];
    notification1.fireDate = newDate1;
    notification1.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
    //    notification1.alertBody = alarmname;//create notification without alert body, just with the sound file, so there are no multiple banners.
    notification1.userInfo = dicttext;

    [[UIApplication sharedApplication] scheduleLocalNotification:notification1];
    totalDuration = totalDuration + [[arraySoundDuration objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]]floatValue];

}
totalDuration = 0;
}