2013-05-11 93 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