2012-03-01 47 views
2

我有2個視圖控制器,我用AVAudioPlayer在第一個視圖控制器中播放音樂。 我去第二視圖控制器和停止音樂:如何讓audioplayer再次玩?

[audioPlayer stop];  
[UIView beginAnimations:@"Curl View" context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 
     [self.view addSubview:secondController.view]; 
     [UIView commitAnimations]; 

當我再回到第一個視圖控制器再次,我想再次播放我的音樂,但代碼不會再碰viewDidLoad中。 這裏是我的代碼回去:

[UIView beginAnimations:@"Flipping View" context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES]; 
    [self.view removeFromSuperview]; 
    [UIView commitAnimations]; 

請分享我的一些解決方案還是什麼that..Thanks後的代碼執行預先

回答

1

寫你在玩viewWillAppear:代碼,它被稱爲當過你的視圖出現。 viewDidLoad:只調用一次的視圖控制器

更新 使用這些行刪除你的第二個觀點

[super viewWillAppear:animated]; 
UIView beginAnimations:@"Flipping View" context:nil]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationEnded)]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view.superview cache:YES]; 
[self.view removeFromSuperview]; 
[UIView commitAnimations]; 

當視圖被刪除

- (void)animationEnded{ 
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/BGMCover2.mp3", [[NSBundle mainBundle] resourcePath]]]; 
    NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = -1; 
    [audioPlayer play]; 
} 
+0

我加void void viewWillAppear,當我從secondController返回時,編譯器不執行viewWillAppear。我從控制檯看它也沒有被編譯器觸及。你能稍微詳細一點嗎? – 2012-03-01 08:54:56

+0

你能否顯示你的viewDidAppear方法定義。 – 2012-03-01 09:22:28

+0

NSLog(@「View Did Appear」);您可以使用NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@「%@/BGMCover2.mp3」,[[NSBundle mainBundle] resourcePath]]]; \t NSError * error; \t audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; \t audioPlayer.numberOfLoops = -1; \t [audioPlayer play]; – 2012-03-01 09:30:33

2

這個方法會被稱爲發生這種情況僅因您正在撥打您的音樂播放代碼viewDidLoadviewDidLoad方法調用只有一次對於ViewController

您應該從viewWillAppear而不是viewDidLoad方法調用您的代碼(播放音樂)。

I suggest you should read UIViewControler Documentation.

更新答:當你想在加載時播放的音樂文件,並即將返回查看。 在性能方面可能存在一些問題。 因爲您立即調用音樂文件加載視圖時間。 查看下面的解釋行。

  • viewDidLoad中 - 視圖控制器加載其視圖層次到內存中後,此方法被調用。無論視圖層次是從nib文件加載還是以編程方式在loadView方法中創建,都將調用此方法。您通常重寫此方法以對從nib文件加載的視圖執行額外的初始化。

  • viewWillAppear中:當這個被調用,這意味着iPhone已經準備好了UIView顯示給用戶,並且任何重物,你在這裏做會非常明顯地影響性能(如被延遲動畫,或播放音樂等)。

  • ViewDidAppear:最後,這會通知其觀點,加入到一個視圖hierarchy.so這種方法對於這樣的任務,例如像播放音樂file.Note足夠好的視圖控制器 - >您必須調用super在你的實現中的某個時刻。

+0

我加了void void viewDidAppear,當我從secondController返回時,編譯器不執行viewDidAppear。我從控制檯看它也沒有被編譯器觸及。你能稍微詳細一點嗎? – 2012-03-01 08:54:29

+0

@Alex Moskalev是否在那個(ViewDidAppear)方法中調用了'super'。你必須在你實現的某個時候調用'super'。 – Kamarshad 2012-03-01 09:01:51

+0

我在第二控制器的[UIView commitAnimations]之前添加了[super viewDidAppear:YES],但它仍然不起作用。我做錯了什麼? – 2012-03-01 09:16:51

0

在做你的應用程序之前,請深入瞭解UIViewController的生命週期。你可以按照下面的鏈接: UIViewController Life Cycle