在我的应用程序中,当我将我的应用程序置于后台模式时无法播放音频..我已经在该站点本身中看到了此类问题。
1)在app-info.plist中添加到后台模式“音频”
2)在代码中,我添加了对音频 session 的支持
if([player isPlaying])
{
[player stop];
}
NSError *error=nil;
int trackid=arc4random()%10+1;
NSString *trackname=[NSString stringWithFormat:@"trk-%d",trackid];
NSLog(@"%@",trackname);
NSString *newAudioFile = [[NSBundle mainBundle] pathForResource:trackname ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:newAudioFile] error:NULL];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) { /* handle the error condition */ }
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) { /* handle the error condition */ }
if(error) {
NSLog(@"%@",&error);
}
[player prepareToPlay];
[player play];
player.numberOfLoops=1;
但是当我将应用程序置于backgroud模式时仍然无法正常工作。当我将应用程序放回原处时它又恢复了。我错过了什么吗?
最佳答案
在-(void)viewDidLoad中使用此行
[UIApplication sharedApplication].idleTimerDisabled = YES;
关于iphone - 在后台模式下播放音频不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6491872/