2014-04-07 2 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에서 소리가납니다. 브레이크 포인트가 없으면 사운드가 재생되지 않습니다. 왜 이런 일이 일어나는거야?위생 시트 탭에서 소리를 재생하는 데 도움이 필요합니다.

+1

메서드가 범위를 벗어나 자마자 플레이어가 할당 취소 되었기 때문에 (로컬 변수로 만든 이후로) 할당이 취소되었습니다. 플레이어에 대한 강력한 속성을 만들면 제대로 작동합니다. – rdelmar

+0

@rdelmar 답장을 보내 주셔서 감사합니다. 답변을 게시하여 정답으로 투표하고 선택하십시오. –

답변

0

프로젝트에 ARC가 사용 설정되어 있다고 생각합니다.

ARC가 메소드 실행 후 로컬 변수를 파괴 할 때.

따라서 AVAudioPlayre 객체를 전역으로 선언해야합니다.

뷰가 파괴 될 때까지 개체를 유지 관리합니다.