本文介绍了支持多任务处理的 iOS 4 闹钟应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个支持多任务处理的闹钟应用.但是,我受困于 sdk 的一些限制.

I'm making an alarm clock app with multitasking support. However, I'm stuck with some limitations of the sdk.

每当闹钟时间带有用户设置的某些属性时,我都需要播放选定的闹钟声音.这些属性:- 警报声可以是用户 iPod 库中的音乐,也可以是应用程序包中的一些声音文件.- 警报声音可以设置为渐进式播放.

I need to play selected alarm sound whenever the alarm time comes with some properties set by the user.These properties:- Alarm sound can be a music from the user's iPod library, and also some sound files in application bundle.- Alarm sound can be set to play as progressive.

此外,警报声必须在后台循环播放,直到用户取消或唤醒应用程序.

Moreover, alarm sound must be played in background in a loop until the user cancels or wakes the app.

我想到的第一个合乎逻辑的事情是使用本地通知,但是使用本地通知,您可以播放仅在应用程序包中的声音文件(不是 iPod 音乐)并且最长 30 秒.此外,当用户取消通知警报时,您不会收到通知,iOS 只是停止播放您的声音.

First logical thing that came to my mind was to use local notifications, but with local notifications you can play sound files that are only in app bundle(not iPod music) and that are at most 30 seconds long. Also you are not get notified when the user cancels the notification alert, iOS just stops playing your sound.

现在我正在考虑使用背景音频播放选项并在闹钟时间之前播放静音,然后在播放闹钟声音的同时也显示没有声音的本地通知.但是我又如何知道用户是否取消了本地通知警报并停止播放音频.然而,根据 Apple 的文档,播放背景音频的应用程序仍然不允许播放 iPod 音乐(和使用共享资源).

Now I'm thinking of using background audio playing option and play silence until the alarm time, and then play the alarm sound while also showing a local notification without sound. But again how will I know if user cancelled the local notification alert and stop playing audio. However according to Apple's documentation iPod music playing(and use of shared resources) is still not allowed for an app that is playing background audio.

我也无法理解其他一些应用程序是如何实现这些功能的.例如,Night Stand HD 可以在后台播放 iPod 音乐,而名为Progressive Alarm Clock"的应用程序可以在后台播放渐进式声音.

I also can't understand how some other apps are doing some of these features. For example, Night Stand HD can play iPod music while in the background, and an app named "Progressive Alarm Clock" can play progressive sound while in the background.

对这些问题有什么想法和建议吗?您的任何帮助将不胜感激

Any ideas and suggestions on these issues? Any of your help will be greatly appreciated

推荐答案

我想说,在 iOS 的当前限制下,你想做的事情是不可能的.也就是说,您可以通过执行 Progressive Alarm Clock 的开发人员为播放渐进式警报所做的事情来伪造渐进式警报.通过安排许多本地通知,一个接一个.他将警报声分成了 10 秒的块,每个块都有渐进的音量级别.这是一个非常粗略的例子,展示了如何伪造渐进式警报.

I would say what you want to do is not possible with the current restrictions of iOS. That said you can probably fake a progressive alarm by doing what the developer of Progressive Alarm Clock do to play the progressive alarm. By scheduling many local notifications, one after each other. He has divided the alarm sounds into chunks of say 10 s each with progressive volume levels. This is a very crude example to show how the progressive alarm can be faked.

UILocalNotification *notif1 = [[UILocalNotification alloc] init];
notif1.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
notif1.soundName = UILocalNotificationDefaultSoundName;
notif1.alertBody = @"Alarm";
[[UIApplication sharedApplication] scheduleLocalNotification:notif1];
[notif1 release];

UILocalNotification *notif2 = [[UILocalNotification alloc] init];
notif2.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];
notif2.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];

这将首先显示一条通知并在 15 秒后播放默认声音.再过 5 秒后,声音将再次播放.通过拥有一堆音量增加的声音文件,可以通过安排更多本地通知来伪造渐进式声音.当然,这只有在你有一个可以很容易地分成块的警报声时才有效,就像渐进式闹钟中的钟声一样.不幸的是,您无法通过点击通知中的取消来取消警报.您必须启动应用程序才能执行此操作.

This will first display a notification and play the default sound after 15 seconds. After 5 seconds more the sound will be played again. By having a bunch of sound files where the volume is increasing the progressive sound can be faked just by scheduling more local notifications. This will of course only work if you have an alarm sound that can be easily divided into chunks, just like the bells in Progressive Alarm Clock. Unfortunately you can't cancel the alarm by tapping cancel in the notification. You have to start the application to do that.

这篇关于支持多任务处理的 iOS 4 闹钟应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:55