iPhone -在后台播放闹铃

时间:2022-09-06 20:55:26

Do you know how to play an alarm sound when the iPhone is sleeping, like the built-in Clock app in iPhone?

你知道如何在iPhone睡觉的时候播放闹铃吗?

VERY IMPORTANT EDIT:
in the built-in Clock app in iPhone
when the alarm sound is playing, if user switch the Silent switch to Silent (Vibrate mode),
the alarm sound still continue to play.

非常重要的编辑:在iPhone内置的时钟app中,当闹铃响起时,如果用户将静音开关切换为静音(振动模式),闹铃还会继续播放。

Do you know how to do the same?

你知道怎么做吗?

5 个解决方案

#1


3  

this code will help you to play music in background: the sound file of the music should be only 30 sec notification support 30 sec sound file and it should be in the bundle folder if it is in the document folder then you have to put the path of the sound file

这段代码将帮助您在后台播放音乐:音乐的声音文件应该只有30秒通知支持30秒声音文件,它应该在文档中包文件夹,如果是文件夹然后你必须把声音文件的路径

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSString *dateValueRemaining =[NSString stringWithFormat:@"23/08/2012 11:30:33"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *dateRemaining = [dateFormat dateFromString:dateValueRemaining];
NSDate *pickerDate = dateRemaining;
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit
                                                         | NSMonthCalendarUnit
                                                         | NSDayCalendarUnit)
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit
                                                         | NSMinuteCalendarUnit
                                                         | NSSecondCalendarUnit)
                                               fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"itemDate: %@", itemDate);
if (localNotif) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
    return;
}
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Your Time is Over";
localNotif.alertAction = @"View";
//---THIS SONG FILE IN THE NSBUNDLE FOLDER---------//
localNotif.soundName = @"s1.mp3";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

#2


2  

The set button is wired up to run a method called scheduleNotification in the view controller which uses the UILocalNotification class to schedule a notification. The code looks as follows:

set按钮被连接起来,在视图控制器中运行一个名为scheduling的方法,该方法使用UILocalNotification类来调度一个通知。代码如下:

(void)scheduleNotification
{
    [reminderText resignFirstResponder];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = [datePicker date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = @"Did you forget something?";
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
              forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

#3


1  

Before the app enters the background, play a silent sound with AVAudioPlayer and keep it play infinite number of times. And when your notification kicks in, again use AVAudioPlayer to play your notification sound, not the UILocalNotification object. It worked for me.

在app进入后台之前,用AVAudioPlayer播放一个无声的声音,并保持无限次播放。当你的通知开始时,再次使用AVAudioPlayer来播放你的通知声音,而不是UILocalNotification对象。它为我工作。

#4


1  

So looking at i-qi app, most likely they've set their UIBackgroundModes to "audio" in their info.plist. This means that they're able to play audio in the background. They're probably playing silent audio until the timer ends because background audio sessions will get cut if they're not in use.

所以看看i-qi应用,他们很可能把他们的uibackgroundmode设置为“audio”在他们的info.plist中。这意味着他们可以在后台播放音频。他们可能会一直播放无声的音频,直到计时器结束,因为如果不使用的话,背景音频会话会被切断。

In terms of the silent switch, thats probably based on the session category they're using. Check out this resource: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

在静默切换方面,这可能是基于他们正在使用的会话类别。查看这个资源:http://developer.apple.com/library/ios/#文档/音频/概念/音频/音频会话程序指南/音频会话目录/音频会话分类/音频会话分类。html#/ apple_ref/doc/uid/TP40007875-CH4-SW1

#5


0  

Only method is to use local Notifications or Push notifications..both allow sounds to be played for 30 seconds

唯一的方法是使用本地通知或推送通知。两者都允许声音播放30秒

#1


3  

this code will help you to play music in background: the sound file of the music should be only 30 sec notification support 30 sec sound file and it should be in the bundle folder if it is in the document folder then you have to put the path of the sound file

这段代码将帮助您在后台播放音乐:音乐的声音文件应该只有30秒通知支持30秒声音文件,它应该在文档中包文件夹,如果是文件夹然后你必须把声音文件的路径

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSString *dateValueRemaining =[NSString stringWithFormat:@"23/08/2012 11:30:33"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *dateRemaining = [dateFormat dateFromString:dateValueRemaining];
NSDate *pickerDate = dateRemaining;
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit
                                                         | NSMonthCalendarUnit
                                                         | NSDayCalendarUnit)
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit
                                                         | NSMinuteCalendarUnit
                                                         | NSSecondCalendarUnit)
                                               fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"itemDate: %@", itemDate);
if (localNotif) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
    return;
}
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Your Time is Over";
localNotif.alertAction = @"View";
//---THIS SONG FILE IN THE NSBUNDLE FOLDER---------//
localNotif.soundName = @"s1.mp3";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

#2


2  

The set button is wired up to run a method called scheduleNotification in the view controller which uses the UILocalNotification class to schedule a notification. The code looks as follows:

set按钮被连接起来,在视图控制器中运行一个名为scheduling的方法,该方法使用UILocalNotification类来调度一个通知。代码如下:

(void)scheduleNotification
{
    [reminderText resignFirstResponder];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = [datePicker date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = @"Did you forget something?";
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
              forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

#3


1  

Before the app enters the background, play a silent sound with AVAudioPlayer and keep it play infinite number of times. And when your notification kicks in, again use AVAudioPlayer to play your notification sound, not the UILocalNotification object. It worked for me.

在app进入后台之前,用AVAudioPlayer播放一个无声的声音,并保持无限次播放。当你的通知开始时,再次使用AVAudioPlayer来播放你的通知声音,而不是UILocalNotification对象。它为我工作。

#4


1  

So looking at i-qi app, most likely they've set their UIBackgroundModes to "audio" in their info.plist. This means that they're able to play audio in the background. They're probably playing silent audio until the timer ends because background audio sessions will get cut if they're not in use.

所以看看i-qi应用,他们很可能把他们的uibackgroundmode设置为“audio”在他们的info.plist中。这意味着他们可以在后台播放音频。他们可能会一直播放无声的音频,直到计时器结束,因为如果不使用的话,背景音频会话会被切断。

In terms of the silent switch, thats probably based on the session category they're using. Check out this resource: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

在静默切换方面,这可能是基于他们正在使用的会话类别。查看这个资源:http://developer.apple.com/library/ios/#文档/音频/概念/音频/音频会话程序指南/音频会话目录/音频会话分类/音频会话分类。html#/ apple_ref/doc/uid/TP40007875-CH4-SW1

#5


0  

Only method is to use local Notifications or Push notifications..both allow sounds to be played for 30 seconds

唯一的方法是使用本地通知或推送通知。两者都允许声音播放30秒