2012-01-03 115 views
0

我使用AVAudioPlayer對象來播放aif聲音文件,除非遊戲結束,否則應該繼續播放。我已經AVAudioPlayer的伊娃稱爲backgroundPlayer,我把它架在viewDidLoad方法:播放AVAudioPlayer導致延遲

-(void)viewDidLoad{ 
NSURL *backgroundURL=[NSURL fileURLWithPath:[[NSBundle   
mainBundle]pathForResource:@"bgLoop" ofType:@"aif"]]; 
backgroundPlayer=[[[AVAudioPlayer alloc] initWithContentsOfURL:backgroundURL  
                   error:nil] retain]; 
backgroundAudioPlayer.numberOfLoops=-1; 

}

然後我在viewWillAppear中方法玩:

-(void)viewWillAppear:(BOOL)animated{  
    TrafficAppDelegate *delegate=[UIApplication sharedApplication].delegate; 
    if (delegate.shouldPlaySFX) 
     [backgroundPlayer play]; 
} 

有了這一切時設置我推動我的按鈕,將視圖控制器推到堆棧,我掛起了大約2-3秒,然後纔出現視圖。如果我在viewWillAppear方法中註釋[backgroundPlayer play]命令,延遲就會消失。我不應該使用AVAudioPlayer類來播放這種聲音嗎?我的意思是聲音可以持續幾分鐘,甚至幾小時。如果選擇不是問題,那麼會導致延遲?在此先感謝

回答

2

您是否在您的代碼中添加了[backgroundPlayer prepareToPlay]後嘗試過這種情況?我相信這將解決延遲問題。

0

發生在你第一次嘗試播放聲音時。您可以在appdelegate中播放短暫的無聲音,以便在您真正需要時響應AVAudioPlayer