2014-04-07 49 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对象为全局的。

它会保持你的对象,直到视图摧毁。