【发布时间】:2014-10-22 19:44:45
【问题描述】:
我的应用程序在后台运行,轮询电子邮件服务器并在发生更改时发送本地通知。我可以从后台状态开始播放警报,但如果手机处于振动状态,则不会播放音频。有没有办法覆盖它,当振动打开时时钟应用程序的闹钟仍然响起?
在启动时我的应用委托中,我调用下面的警报类方法来设置在后台播放:
+ (void)initializeAudioSettings
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) {
NSLog(@"Error setting up Audio session: %@", setCategoryError.description);
}
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) {
NSLog(@"Error setting audio session active: %@", activationError.description);
}
}
接下来我加载警报:
- (void)loadAlertSoundID
{
NSLog(@"Loading alert sound");
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"alert" ofType:@"mp3"];
NSURL *soundURL = [NSURL URLWithString:soundPath];
CFURLRef url = (__bridge CFURLRef)soundURL;
AudioServicesCreateSystemSoundID(url, &_alertSoundID);
}
然后当我收到新消息的通知时,我会调用我的警报方法:
- (void)playAlert
{
NSLog(@"Playing alert");
AudioServicesPlaySystemSound(_alertSoundID);
_alertShouldBePlaying = YES;
_alertIsPlaying = YES;
AudioServicesAddSystemSoundCompletion(_alertSoundID, nil, nil, playSoundFinished, (__bridge void*)self);
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(vibrate:) userInfo:nil repeats:YES];
}
为了完成,我的完成块。:
void playSoundFinished (SystemSoundID sound, void *data)
{
Alert *alert = [Alert sharedAlert];
if (sound == alert.alertSoundID)
{
alert.alertIsPlaying = NO;
// Repeat until alert is disabled
if (alert.alertShouldBePlaying && !alert.alertIsPlaying)
{
[alert playAlert];
}
}
}
【问题讨论】:
-
这是“把我赶出剧院”功能吗?
-
我正在构建它作为在办公室内部使用的工具。有一个“下班”选项,它专门切换到振动,然后在第二天工作开始时自动重新上班。
-
将手机置于振动状态的全部意义在于它不会发出声音。
-
我知道,但就像我说的,这是供内部使用,而不是去应用商店。必须有办法做到这一点,因为时钟应用正是这样做的
-
Apple 应用程序可以做某事并不意味着第三方应用程序可以使用公共 API。时钟应用也可以更新其图标,但第 3 方应用不能这样做。
标签: ios objective-c core-audio audiotoolbox