2013-03-10 1 views
0

나는 Xcode에 초보자이며 xcode를 마스터 한 경험이 필요합니다. UIActionSheet를 사용하여 음악을 재생, 일시 중지, 중지하려고하지만 일시 중지 및 중지 버튼을 클릭해도 멈추지 않거나 재생 버튼 만 일시 중지합니다. 누군가 나를 도와 줄 수 있습니까? 내 코드는 다음과 같습니다.UIActionSheet로 음악 재생, 일시 정지, 정지

#import <AVFoundation/AVFoundation.h> 
@interface MainViewController() <UIActionSheetDelegate,AVAudioPlayerDelegate> 
@property(nonatomic,retain)AVAudioPlayer *playAudio; 
@end 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *buttonTitle =[actionSheet buttonTitleAtIndex:buttonIndex]; 
    NSURL *url =[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/MusicName.mp3",[[NSBundle mainBundle] resourcePath]]]; 
    NSError *error; 

    self.playAudio=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error]; 

    if (buttonIndex==0) { 

     [self.playAudio play]; 

    } 
    if ([buttonTitle isEqualToString:@"Pause"]) { 

     [self.playAudio pause]; 
    } 
    if (buttonIndex==2) { 
     [self.playAudio stop]; 
    } 
} 
- (IBAction)Music:(id)sender { 

    UIActionSheet *musicSheet =[[UIActionSheet alloc]initWithTitle:@"Choose" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Play",@"Pause",@"Stop", nil]; 
    [musicSheet showInView:self.view]; 


} 

. 나는 베트남 사람이므로 내 영어 실력으로는 내가하는 말을 이해하기 어렵게 만들 수 있습니다. 시간 내 주셔서 감사합니다 . 귀하의 회신을 기다리고 있습니다.

답변

0

우선 : 버튼 제목에 의지해서는 안됩니다. 버튼 인덱스를 사용하여 어떤 버튼이 눌려 있는지 식별 할 수 있습니다.

두 번째 : 버튼 인덱스가 맞습니까? switch 문을 사용해보십시오.

같은 : 다음 시도 할 것이다 당신의 대답에 대한

switch (buttonIndex) { 
     case 0: 
      [self.playAudio play]; 
      break; 
     case 1: 
      [self.playAudio pause]; 
      break; 
     case 2: 
      [self.playAudio stop]; 
      break; 
     default: 
      break; 
} 
+0

TKS 내가 당신에게 나중에 – Triet

+0

말해주지 내가 스위치 statement.Do을 사용하는 경우 ... 아니 아직도 내가 위양을 설정하는 데에도 작동하지 각 buttonIndex? switch 문 전에 대리자를 설정하려고했지만 여전히 작동하지 않습니다. – Triet

+0

왜 UIButton에서 작동합니까 ????? UIActionSheet 버튼이 아닙니까? – Triet