在背景ios app中播放音乐
在我的代码中:
- (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]; }
但是当我在模拟器中运行我的应用程序时,没有音乐播放。 请帮忙!
用它来播放背景音频:
[[AVAudioSession sharedInstance] setDelegate: self]; NSError *setCategoryError = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
参考此链接
这个
www/media/BlueZedEx
不是文件名。 它必须是项目内音频文件的文件名(拖放)