2010-10-09 3 views
0

Je cherche à arrêter la lecture d'un fichier audio lorsque la vue est modifiée.viewDidUnload - arrêt d'une méthode lorsque la vue est modifiée

J'utilise un tabController et je souhaite que l'audio en cours d'exécution s'arrête lorsque l'utilisateur passe à une autre vue. Je ne sais pas où et comment je ferais ça. dans le viewDidUnload peut-être?

est ici la méthode que je utilise pour lire le fichier audio:

- (void) startPlaying { [NSTimer scheduledTimerWithTimeInterval: 15 cible: sélecteur auto: @selector (startPlaying) userInfo: néant répète: NO] ; NSString * audioSoundPath = [[NSBundle mainBundle] pathForResource: @ "fichier_audio" deType: @ "caf"]; CFURLRef audioURL = (CFURLRef) [NSURL fileURLWithPath: audioSoundPath]; AudioServicesCreateSystemSoundID (audioURL, & audioID); AudioServicesPlaySystemSound (ID audio); }

Merci pour toute aide

Répondre

2

quelque chose comme ça dans votre contrôleur de vue (non testé):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Load sample 
    NSString *audioSoundPath = [[NSBundle mainBundle] pathForResource:@"audio_file" 
                   ofType:@"caf"]; 
    CFURLRef audioURL = (CFURLRef)[NSURL fileURLWithPath:audioSoundPath]; 
    AudioServicesCreateSystemSoundID(audioURL, &audioID) 
} 

- (void)viewDidUnload 
{ 
    // Dispose sample when view is unloaded 
    AudioServicesDisposeSystemSoundID(audioID); 

    [super viewDidUnload]; 
} 

// Lets play when view is visible (could also be changed to viewWillAppear:) 
- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [self startPlaying]; 
} 

// Stop audio when view is gone (could also be changed to viewDidDisappear:) 
- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    if([self.audioTimer isValid]) { 
     [self.audioTimer invalidate]; 
    } 
    self.timer = nil; 
} 

// Start playing sound and reschedule in 15 seconds. 
-(void)startPlaying 
{ 
    self.audioTimer = [NSTimer scheduledTimerWithTimeInterval:15 target:self 
                selector:@selector(startPlaying) 
                userInfo:nil 
                 repeats:NO]; 
    AudioServicesPlaySystemSound(audioID); 
} 

manquant:

  • Vérification des erreurs
  • propriété ou Ivar pour audioTimer .
  • Pourrait également conserver la même minuterie, avec des répétitions OUI.
  • Libérer des ressources dans dealloc.
  • Test
+0

merci pour votre message. – hanumanDev

+0

Vous êtes les bienvenus. – dlundqvist

Questions connexes