使用 AVAudioplayer 在后台播放音乐

编程入门 行业动态 更新时间:2024-10-26 12:24:31
本文介绍了使用 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.

我使用了以下代码:-

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 在后台播放音乐

本文发布于:2023-07-11 09:04:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   音乐   AVAudioplayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!