2014-04-07 55 views
0
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
if(actionSheet.tag == 2) { 
     if(buttonIndex != actionSheet.cancelButtonIndex) { 
      NSString *s = [actionSheet buttonTitleAtIndex:buttonIndex]; 
      NSString *soundFile = [[NSBundle mainBundle] pathForResource:s ofType:@"m4a"]; 
      NSURL *soundUrl = [NSURL fileURLWithPath:soundFile]; 
      AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundUrl error:nil]; 
      player.numberOfLoops = 0; 
      [player play]; 
     } 
    } 
} 

播放聲音當我把在[player play];斷點,單步執行,iPhone正在播放的聲音。沒有斷點就不會播放聲音。爲什麼發生這種情況?需要幫助的uiactionsheet自來水

+1

因爲只要方法超出範圍,您的播放器就會被釋放(因爲您將其作爲局部變量創建)。爲玩家創造一個強大的財產,它會正常工作。 – rdelmar

+0

@rdelmar感謝您的回覆,請將其發佈爲答案,以便我可以投票並選擇正確的答案。 –

回答

0

我認爲你的項目啓用ARC。

當ARC啓用局部變量在方法執行後銷燬。

所以你必須聲明你的AVAudioPlayre對象爲全局的。

它會保持你的對象,直到視圖摧毀。