2010-10-09 3 views
0

Я ищу, чтобы остановить воспроизведение аудиофайла при изменении вида.viewDidUnload - остановка метода при изменении вида

Я использую tabController, и я хотел бы, чтобы звук, который воспроизводится, останавливался, когда пользователь переместился в другое представление. Я не знаю, где и как я это сделаю. возможно, в viewDidUnload?

здесь метод я использую для воспроизведения аудио файлов: не

- (Недействительными) startPlaying { [NSTimer scheduledTimerWithTimeInterval: 15 Цель: селектор самостоятельно: @selector (startPlaying) USERINFO: ноль повторами: NO] ;

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

спасибо за любую помощь

ответ

2

что-то подобное в вашем контроллере представления (непроверенные):

- (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); 
} 

Missing:

  • Ошибка проверки
  • Свойство или Ивар для audioTimer ,
  • Также можно сохранить один и тот же таймер, с повторами ДА.
  • Освобождение ресурсов в dealloc.
  • Тестирование
+0

благодарит за сообщение. – hanumanDev

+0

Добро пожаловать. – dlundqvist

Смежные вопросы