0

こんにちは私はUINavigationControllerを使用するHomeViewControllerというUIViewを持っています。音のものを扱う//ビューが表示される前にAVAudioPlayerが再生されないようにする方法

:GameViewControllerさんのviewDidLoadで

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

、私は別の後に2つのオーディオファイル1を再生するには、次のコードを使用します。ボタンをクリックするだけで、私は次のコードを使用して別のビューをプッシュ

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ののviewDidLoadから呼び出されているにもかかわらず、HomeViewControllerに残っている間に再生するために始めた理由私は理解することはできません。

私は次のコードを削除する場合:

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

を最初のオーディオファイルがGameViewControllerがロードされたときに再生するために始めたが、第二のオーディオファイルのオーバーラップ。

ありがとうございました。

答えて

1

HomeViewControllerのボタンまたはテーブルビューのセルをクリックすると、オーディオファイルが再生されますか?のように、GameViewControllerのビューが右から左にアニメーション化されている間に再生を開始しますか?もしそうなら、ビューが実際に提示される前に-viewDidLoadが呼び出されているからです。代わりに、ビューロジックを-viewDidAppear:に配置する必要があります。これは、ビューのアニメーションが終了して完全に表示されたときにのみ呼び出されます。

+0

何が起こっていたのか正確に記載されています。私はコードを-viewDidAppearに移動しました。それは問題を解決するようです。 – atbebtg

1

viewDidLoadからviewDidAppear:にコードを移動できます。

+0

正しいメソッドシグネチャは '-viewDidAppear:'です。 'animated'は(通常)メソッド引数の名前です。 – LucasTizma

関連する問題