2011-04-06 190 views
0

我有一個聲音,它在應用程序啓動時啓動,它的長度爲30秒。無論我在哪個視圖控制器中,如何在重播歌曲時重新播放歌曲?當應用程序啓動時,聲音應該再次啓動

我的代碼:

-(void)playBgMusic { 

NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"]; 
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
theAudio.delegate = self; 
[theAudio play]; } 


-(id)initWithCoder:(NSCoder *)coder { 

self = [super initWithCoder:coder]; 

if(self) 
{ 
    [self playBgMusic]; 
} 

return self; } 

回答

2

嘗試

theAudio.numberOfLoops = -1 

Apple's Documentation獲取更多信息。

audioPlayer實例的所有者應該是appDelegate,然後讓它播放用戶在應用中的任何位置。

+0

謝謝,當我把它放在appdelegate中時出現錯誤,但它也在我的viewcontroller中工作 – Leon 2011-04-06 18:43:15

相關問題