【发布时间】:2013-11-24 03:38:25
【问题描述】:
我正在构建一个正在运行的应用程序,它会每隔几分钟通过音频通知用户他们需要开始跑步或开始步行。即使在使用 AVAudioPlayer 锁定屏幕的情况下,我也能够让声音在后台运行。
这是我所拥有的sn-ps:
ViewController.h
@property (nonatomic, strong) AVAudioPlayer *audioWalk;
ViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
// Walk Audio File
NSString *soundFile2 = [[NSBundle mainBundle] pathForResource:@"Walk" ofType:@"wav"];
audioWalk = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile2] error:nil];
// Load the audio into memory
[audioWalk prepareToPlay];
// Permit the timer to run in the background
bgTask = 0;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
// Prevent the application from going to sleep while it is running
[UIApplication sharedApplication].idleTimerDisabled = YES;
// Starts recieving remote control events and is the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
// Plays audio
[audioWarmup play];
}
我正在尝试弄清楚如何在不中断后台播放的音乐的情况下播放我的音频。此外,声音需要在后台和屏幕锁定时播放。任何帮助将不胜感激!
【问题讨论】:
标签: ios objective-c audio