2012-02-08 72 views
1

我有一個應用程序包含三個按鈕,播放,暫停和停止。在viewDidLoad我做到以下幾點:iOS AVAudioPlayer

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:songName ofType:@"mp3"]; 
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 

[playBtn addTarget:self action:@selector(playSong) forControlEvents:UIControlEventTouchUpInside]; 
[pauseBtn addTarget:self action:@selector(pauseSong) forControlEvents:UIControlEventTouchUpInside]; 
[stopBtn addTarget:self action:@selector(stopSong) forControlEvents:UIControlEventTouchUpInside]; 

的playSong功能是:

-(void) playSong 
{ 
    [player play]; 
} 

如果我按下播放按鈕時,文件被加載,並保持按下約3秒鐘,然後在音樂開始播放。當按下播放按鈕時,是否有辦法讓背景音樂立即播放?

回答

5

先前調用[player prepareToPlay];預加載剪輯。