2012-11-24 162 views
0

我想創建一個大喊的遊戲。當用戶按下喊話按鈕時,應播放聲音。我有守則。但我的問題是:如果用戶觸摸按鈕,聲音播放良好,但如果我按下按鈕的速度很快,按鈕變爲突出顯示圖像緩慢。如果我在ViewDidLoad中使用我的代碼,按鈕圖像變得很好,非常快,但聲音播放緩慢,延遲。按鈕觸摸播放聲音

這裏是我的代碼:

NSString *path = [[NSBundle mainBundle] pathForResource:@"tap" ofType:@"mp3"]; 
    AVAudioPlayer *playOn =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

    playOn.delegate=self; 


    [playOn play]; 

回答

0

照你說你需要發揮按鈕單擊文件,因此在這裏你應該使用按鈕點擊小音頻文件

這裏假設如下方法被調用點擊按鈕。

- (void)playAudio{ 

    if([player isPlaying]) 
      { 

      [self stopAudio]; 
      } 
    } 
    /* Use this code to play an audio file */ 
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:pathOfFile ofType:@"mp3"]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
    player.numberOfLoops = 0; //o for play only once that file. 
    player.delegate= self; 
    isAudioPlaying = YES; 
    [player play]; 
    //player is instance of AvAudioPlayer. 
    //here write code for setting aND CHANging the image. 
} 


- (void)stopAudio 
{ 
if (player!= nil) { 
    [player stop]; 
//do some task for changing the Image i.e setting the default image 
} 

}