2011-10-02 76 views
0

因此,我幾乎完成了我的應用程序,並且我碰到了一堵牆,無法繞過這個最後的問題。AVAudioPlayer通知問題

我基本上有2個視圖。主頁面和遊戲畫面。

在第一個玩遊戲的時候可以正常工作,但是當我離開主屏幕然後返回到遊戲屏幕時,所有的聲音都會複製並且提前觸發playerItemDidReachEnd(因爲我猜測它有2個實例,但它似乎無法讓玩家停止此操作。下面是導致此問題的基本代碼。任何幫助將會很長,謝謝。我不確定是否我的問題是我在View1中創建View2的多個實例或者如果我在view2中創建多個播放器對象,從而複製通知。 我知道我的 - (void)playerItemDidReachEnd:(NSNotification *)通知有很多事情發生,但它在第一次加載頁面時工作正常,它只有當我點擊「回到view1」,然後回到View2時發生問題。

View1ViewController.h 
---------------------- 
#import "(i know here are arrows here, but can't make them show)UIKit/UIKit.h> 
#import "ApplicationViewController.h" 

@interface MonsterSpellViewController : UIViewController { 
} 
-(IBAction)showView1; 

View2ViewController.m 
---------------------- 
-(IBAction)showView2{ 

    ApplicationViewController *view2 = [[ApplicationViewController alloc]initWithNibName:@"ApplicationViewController" bundle:nil]; 

    view2.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:view2 animated:YES]; 

} 



View2ViewController.h 
------------------------ 
#import "(i know here are arrows here, but can't make them show)UIKit/UIKit.h> 
#import "(i know here are arrows here, but can't make them show)AVFoundation/AVFoundation.h> 

@class AVAudioPlayer; 

@interface ApplicationViewController : UIViewController{ 

AVAudioPlayer *avPlayer; 
} 

View2ViewController.m 
------------------------- 
#import "View2ViewController.h" 

@synthesize avPlayer; 

-(AVAudioPlayer *)avPlayer { 
    if(!avPlayer) avPlayer = [[AVAudioPlayer alloc]init]; 
    return avPlayer; 
} 

-(void) viewDidLoad 
{ 

[[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(playerItemDidReachEnd:) 
    name:AVPlayerItemDidPlayToEndTimeNotification 
    object:avPlayer]; 
} 

-(IBAction)playSoundTest 
{ 
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/monster3.mp3", [[NSBundle mainBundle] resourcePath]]]; 

    self.avPlayer = [AVPlayer playerWithURL:url]; 

    [self.avPlayer play]; 
} 

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
    //[player seekToTime:kCMTimeZero]; 
    if (playEscape == YES) { 
     [self btnEscapeSound]; 
     playEscape = NO; 
    } 
    if ((startButton.hidden == NO) && (letterCount != -1) && (playFinal == YES)) 
    { 
     [self btnFinalSound]; 
     playFinal = NO; 
     playEscape = YES; 
    } 

    NSLog(@"Player Check accessed"); 
    if (letterCount == 3) { 

     if (Winner == letterCount) { 
      //moveNextSound = YES; 
      if (intLoopCount < 104) 
      { 
       if (intLoopCount<104) { 


        [self btnStartOver:intLoopCount]; 
        playFinal = NO; 
        //intLoopCount++; 
       } 
       if (intLoopCount==104) { 
        startButton.hidden=NO; 
        playFinal = YES; 

       } 
      } 
     } 
    } 

    if (letterCount == 4) { 

     if (Winner == letterCount) { 
      //moveNextSound = YES; 
      if (intLoopCount < 105) 
      { 
       [self btnStartOver:intLoopCount]; 
       //intLoopCount++; 
       if (intLoopCount==105) { 
        startButton.hidden=NO; 
        playFinal = YES; 
       } 
      } 
     } 
    } 





} 
} 

-(IBAction)goBack:(id)sender{ 


    [self dismissModalViewControllerAnimated:YES]; 

} 

回答

0

嘗試添加[view2 release]; after [self presentModalViewController:view2 animated:YES];