2011-10-02 3 views
0

J'ai presque terminé avec mon application et j'ai atteint un mur et je n'arrive pas à contourner ce dernier problème.Problème de notification AVAudioPlayer

J'ai fondamentalement 2 vues. Une page principale et l'écran de jeu. Sur la première partie, le jeu fonctionne bien, mais quand je quitte l'écran principal et que je retourne à l'écran de jeu, tous les sons se dupliquent et déclenchent le playerItemDidReachEnd tôt (parce que je devine qu'il y en a 2 occurrences) , mais il ne semble pas que le lecteur arrête ça.Voici le code de base à l'origine.Chaque aide irait un long chemin, merci.Je ne suis pas sûr si mon problème est que je crée plusieurs instances de View2 dans View1 ou si je crée des objets multi-joueurs dans view2 en dupliquant ainsi la notification Je sais qu'il se passe beaucoup de choses dans ma notification - (void) playerItemDidReachEnd: (NSNotification *), mais cela fonctionne bien sur le premier chargement de la page , c'est seulement quand je clique sur "revenir à view1" et puis revenez à View2 que le problème se produit

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]; 

} 

Répondre

0

Essayez d'ajouter [view2 release]; après [self presentModalViewController: view2 animé: YES];