本文介绍了使用AVAudioplayer在后台播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使应用程序进入后台,我也想播放音乐。我检查了所有stackoverflow链接,但没有一个工作。今天请帮忙完成。
I want to play music even if the app goes in background. I checked all stackoverflow links but none of them worked. Please help need to do it today.
我使用了以下代码: -
I had used following code:-
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"Day At The Beach" ofType: @"mp3"]; NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; NSError *error; playerTemp = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; playerTemp.numberOfLoops = 0; AudioSessionSetActive(true); [playerTemp play];推荐答案
我通过引用 iOS应用程序后台任务
并在我们的应用程序的 .plist 文件中进行一些更改..
and make some changes in .plist file of our application..
更新
在您的控制器视图中写下此代码执行此类加载方法
write this code in your controller's view did load method like this
- (void)viewDidLoad { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"in-the-storm" ofType:@"mp3"]]; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [audioPlayer play]; [super viewDidLoad]; }更多推荐
使用AVAudioplayer在后台播放音乐
发布评论