我正在开发一个 iOS 应用程序,它使用 MPMusicPlayerController 来播放 iTunes 库中的音频。
是否可以创建一个在应用程序处于后台时运行的计时器?我想实现一个 sleep 定时器。 NSTimer 似乎是不可能的,或者更确切地说仅限于 3 分钟。我可以尝试其他选择吗?
目前,我的 App Delegate 中有这个。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
return YES;
}
最佳答案
要在使用 MPMusicPlayerController 时保持应用程序在后台运行,您可以在后台播放静音。这可能是个坏主意......
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSString* path = [[NSBundle mainBundle] pathForResource:@"silence" ofType:@"m4a"];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:nil];
self.player.numberOfLoops = -1;
[self.player play];
return YES;
}
关于ios - 带 MPMusicPlayerController 的 sleep 定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31713175/