2013-05-11 87 views
1

在我的代码播放音乐:背景iOS应用

- (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]; 

} 

但是当我运行我在模拟器的应用程序没有什么音乐播放。 请帮忙!

回答

0

www/media/BlueZedEx 

不是一个文件名。 它需要你的项目内的音频文件(拖放)

1

使用的文件名这对播放背景声音:

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

指这个link

+0

请给我更清楚你的回答,我正在做我的第一个iOS应用程序。我如何使用上面的代码,以及在使用此代码时如何播放音乐。 – 2013-05-11 06:19:30

+0

在' - (void)viewDidLoad'上使用上面的代码。当设备被锁定时,它将处理播放背景音频。 – Ashini 2013-05-11 06:42:54

+1

哦,但我想要从1个屏幕播放音乐,当我进入2个屏幕时,此音乐仍然播放,不会再次重新加载。我怎么能这样做。谢谢 – 2013-05-11 07:49:40