私のアプリケーションは、 'App plays audio'に設定されたUIBackgroundModes
を使用するための要件を満たしています。私のアプリケーションでは、MPMusicPlayerController iPodMusicPlayer
クラスの音楽が再生されます。私が達成しようとしているのは、ユーザーが音楽を停止するタイマーを設定できるようにすることです。私はこれに問題があります。 checkTimeRemaining
方法でバックグラウンドサービスは「バックグラウンドで実行中」を許可されています。では、なぜこれを動作させることができないのですか?
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
backgroundSupported = device.multitaskingSupported;
}
if (backgroundSupported) {
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
[app endBackgroundTask:bgTask];
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (bgTask != UIBackgroundTaskInvalid) {
if (rootController.sleepTimer != nil)
self.sustainTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkTimeRemaining) userInfo:nil repeats:YES];
}
});
});
}
}
:次のように私はapplicationDidEnterBackground
方法でバックグラウンドタスクを実装しました
-(void)checkTimeRemaining{
NSTimeInterval totalSeconds = rootController.timerSetInSeconds.doubleValue;
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:rootController.startDateforSleepTimer];
NSTimeInterval remainingTime = totalSeconds - elapsedTime;
NSLog(@"remaining time %f",remainingTime);
if (remainingTime <= 0) {
[self.musicPlayer pause];
[sustainTimer invalidate];
self.sustainTimer = nil;
}
}
私はすべてのエラーを得ていないんだけど、タイマーは超えません10分を許容した。私はオーディオを再生しているので(バックグラウンドモードのセットと一緒に)、指定された時間に音楽を止めることができます。私がどこに間違っているかについてのアイデアは?
おかげ
こんにちは。私がまだ 'iPodMusicPlayer'コントローラを使用しているかどうかをあなたが記述していますか?これを実装する方法を学び始めるのに最適な場所はどこですか?これがスリープタイマーアプリがこれを達成している様子です。彼らはまだiPodの音楽コントローラを使用することができます。 – sooper