2012-03-28 62 views
1

可能重複:目前
iOS SDK : playing music on the background and switching viewsiOS的背景音樂問題

嗨,大家好即時通訊使用此代碼在我的應用程序來播放背景音樂時,它的推出。但當我切換視圖我SecondView並返回到我的Firstview,音樂重新開始並覆蓋當前正在播放的音樂,並且當我返回到我的Firstview時它會保持重疊的音樂,當我從我的第二個視圖切換回我的Firstview時,如何讓音樂重新開始。

FirstViewController.m

- (void)viewDidLoad { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
+0

'theAudio'是什麼? – 2012-07-10 11:29:47

回答

0

您重新分配並重新創建每次顯示視圖時的AVAudioPlayer。如果可能,你應該避免這種情況作爲您的問題的解決方案,取決於您的代碼,請致電viewDidDisappearviewDidUnload事件中的[theAudio stop]。這會在視頻進入後臺時停止音頻播放器。

+0

是的,你可以在viewDidDisappear或viewWillDisappear中調用[theAudio stop]。但不是在viewDidUnload,因爲它只有當任何內存警告提出時調用,否則... – 2012-03-28 08:40:19

+0

以及我需要背景音樂跨所有視圖播放,並需要音樂重新啓動時,我打我的首頁按鈕回到我firstview。我嘗試了ViewDidDisappear下的[音頻停止],但它在切換視圖時停止音樂,我不需要該選項。 – 2012-03-29 22:29:36

1

兩個選項,

1-當第一個視圖消失時停止播放器。

[theAudio stop] in `viewWillDisappear` 

2-如果你不想再回來玩。 在appDelegate類中聲明theAudio。並在分配之前檢查它是否被分配。

if(!appDelegate.theAudio) 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    appDelegate.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
} 
else 
{ 

[theAudiostop]; 
    [theAudio play]; 
} 
+0

以及我需要背景音樂播放所有視圖,並需要音樂重新啓動時,我打我的首頁按鈕回到我的firstview。目前我加入[theAudio prepareToPlay];到 - (void)viewWillDisappear:(BOOL)animated {..當我點擊主頁按鈕回到我的FirstView音樂繼續播放,它工作正常,但我想要背景音樂重新啓動時,我打我的主頁按鈕回到我的FirstView。 – 2012-03-29 22:37:13

3

您可以使用NSUserDefaults的保存音樂播放狀態,這樣的觀點在加載時,音樂將只如果有尚未發揮。

if ([[NSUserDefaults standardUserDefaults] stringForKey:@"music"] == nil) { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; } 

當退出應用程序時,您需要將此鍵重置爲零。否則,如果玩家退出應用程序,並且下次重新啓動應用程序時,@「music」字符串不會爲零。

轉到projectAppDelegate.m文件:

- (void)applicationDidEnterBackground:(UIApplication *)application { 
     [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"music"]; } 

如果音樂被整個應用播放,然後把音樂,如下圖所示是安全的打碼。不要忘記在projectAppDelegate.h文件中聲明AVAudioPlayer * theAudio。

- (void)applicationDidBecomeActive:(UIApplication *)application { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drums" ofType:@"mp3"]; 
    theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; } 

我可能在這裏錯過了幾位。只需運行一些測試,使其成爲您喜歡的工作方式。我只是嘗試了我的應用程序,它爲我工作。