简体   繁体   English

在后台iOS应用中播放音乐

[英]Playing music in background ios app

In my code: 在我的代码中:

- (void) viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"www/media/BlueZedEx" ofType:@"mp3"]];

AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];

NSLog(@"goldenace launchOptions = %@", musicFile);
click.delegate = self;
click.numberOfLoops = -1;
[click play];

}

But nothing music play when I run my app in simulator. 但是当我在模拟器中运行我的应用程序时,没有音乐播放。 Please help! 请帮忙!

use this for Playing Background Audio: 使用它来播放背景音频:

 [[AVAudioSession sharedInstance] setDelegate: self];
 NSError *setCategoryError = nil;
 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

refer this link 参考此链接

This 这个

www/media/BlueZedEx

is not a filename. 不是文件名。 It needs to be a filename of a audio file inside your project (drag and drop) 它必须是项目中音频文件的文件名(拖放)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM