0

안녕하세요 저는 UINavigationController를 사용하는 HomeViewController라는 UIView가 있습니다. 사운드 물건을 처리 할 //보기가 표시되기 전에 AVAudioPlayer가 재생되지 않도록하는 방법

: GameViewController의의 viewDidLoad에서

gameView = [[GameViewControler alloc] init]; 
    [[self navigationController] pushViewController:gameView animated:YES]; 
    [gameView release]; 

, 나는 또 다른 후 2 개 오디오 파일을 재생하려면 다음 코드를 사용 버튼 클릭에, 나는 다음과 같은 코드를 사용하여 다른보기를 밀어 첫 번째 오디오 파일이 화면이 GameViewController의 viewDidLoad에에서 호출되는 경우에도 HomeViewController에있는 동안 연주하기 시작하는 이유

if([[GameStore defaultStore] currentIndex] == 0) 
{ 
    if(![audioPlayer isPlaying]) 
    { 
     NSString *findPath = [[NSBundle mainBundle] pathForResource:@"findtheword" ofType:@"aiff"]; 
     if(findPath) 
     { 
      NSURL *findURL = [NSURL fileURLWithPath:findPath]; 
      NSError *findError; 
      audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:findURL error:&findError]; 

      if(audioPlayer == nil) 
      { 
       NSLog([findError description]); 
      } 
      else{ 
       [audioPlayer play]; 
      } 
     } 
    } 
} 

while ([audioPlayer isPlaying]) { 
    //do nothing and wait so the sound is not overlap 
} 


if(![audioPlayer isPlaying] || ([audioPlayer isPlaying] && override)){ 
    Word *currentWord = [[GameStore defaultStore] currentWord]; 
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:[currentWord fileName] 
                 ofType:[currentWord fileType]]; 
    // If this file is actually in the bundle... 
    if (soundPath) { 
     // Create a file URL with this path 
     NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; 

     NSError *error; 
     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]; 

     if (audioPlayer == nil) 
      NSLog([error description]); 
     else 
      [audioPlayer play]; 
    } 
    [FlurryAnalytics logEvent:@"GameViewController - playSound"]; 
} 
else{ 
    [FlurryAnalytics logEvent:@"GameViewController - playSound is still playing"]; 
} 

나는 알아낼 수 없습니다.

나는 다음과 같은 코드를 제거 할 경우 : 첫 번째 오디오 파일이 GameViewController로드하지만 2 오디오 파일 중복 될 때 재생하기 시작

while ([audioPlayer isPlaying]) { 
    //do nothing and wait so the sound is not overlap 
} 

합니다.

대단히 감사합니다.

답변

1

HomeViewController의 단추 또는 테이블보기 셀을 클릭하면 오디오 파일이 재생되기 시작합니까? 마찬가지로, GameViewController의보기가 오른쪽에서 왼쪽으로 움직이는 동안 재생되기 시작합니까? 그렇다면 뷰가 실제로 표시되기 전에 -viewDidLoad이 호출되기 때문입니다. 대신보기 논리를 -viewDidAppear:에 배치하면됩니다. 뷰가 애니메이션으로 완성되고 완전히 볼 수있을 때만 호출됩니다.

+0

정확히 무엇이 일어 났는지 설명했습니다. 코드를 -viewDidAppear로 옮겼습니다. 문제가 해결 된 것 같습니다. – atbebtg

1

viewDidLoad에서 viewDidAppear:으로 코드를 이동할 수 있습니다.

+0

올바른 메소드 서명은'-viewDidAppear :'입니다. 'animated'는 (일반적으로) 메소드 인수의 이름입니다. – LucasTizma

관련 문제