我想在特定的時間播放音頻文件,我已經set.for,我已經在info.plist中的「UIBackgroundModes」和didEnterBackground.Its中的backgroundtaskidentifier添加音頻工作在模擬器,但不是在device.Can任何人都可以幫助解決此問題。如何在iPhone中播放背景音頻?
在此先感謝...
我想在特定的時間播放音頻文件,我已經set.for,我已經在info.plist中的「UIBackgroundModes」和didEnterBackground.Its中的backgroundtaskidentifier添加音頻工作在模擬器,但不是在device.Can任何人都可以幫助解決此問題。如何在iPhone中播放背景音頻?
在此先感謝...
你可以看看這個reference app with streaming and backgrounding of audio。
編輯:
一個本地通知瓦特/附加的聲音的實施例。 More detail here。
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// bgTask is instance variable
NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
NSString *friend = [self checkForIncomingChat];
if (friend) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:
NSLocalizedString(@"%@ has a message for you.", nil), friend];
localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
localNotif.soundName = @"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
friend = nil;
break;
}
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}
如果我理解正確的話,你要在特定時間播放聲音,無論你的應用程序是活動的,當時前景化?您唯一的(應用程序審查友好)選項是安排本地通知,如slf的答案。 UIBackgroundModes不會幫助你:音頻背景模式僅用於當你預先播放音頻時開始播放時允許執行代碼,而後臺播放。示例用法是音樂播放器和廣播應用程序。
如果您更徹底地解釋您的用例,我可能會有更好的答案。
看到這個http://stackoverflow.com/questions/7619794/play-music-in-background-in-iphone-using-avaudioplayer/7619816#7619816 – 2012-03-09 07:14:02