2012-12-17 4 views
1

동일한 코드로 play/Pause 버튼을 만드는 방법.동일한 버튼으로 재생/일시 정지

- (IBAction)min:(id)sender 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"]; 
    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
        theAudio.delegate = self; 
        theAudio.numberOfLoops = -1; 
        [theAudio play]; 
        [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; 
} 

동일한 버튼으로 어떻게 다시 시작할 수 있습니까?

답변

7

사용이 버튼의 상태를 확인하려면

UIButton *button = (UIButton *)sender; 

button.selected = !button.selected; 

if(button.selected) 
{ 
    // Play 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"1min" ofType:@"mp3"]; 
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 
    theAudio.delegate = self; 
    theAudio.numberOfLoops = -1; 
    [theAudio play]; 
    [[NSUserDefaults standardUserDefaults] setObject:@"-" forKey:@"music"]; 
} 
else 
{ 
    // Pause 
    [theAudio pause]; 
} 
+0

가 // 재생에 대한 자세한 내용을 적어주세요 수 있고 // 부분을 일시 중지 하시겠습니까? –

+0

@ 장성진 업데이트 된 답변을 시도하십시오. –

+0

고맙습니다. –

0

부울을 만듭니다 (예 : buttonIsPlayButton). 버튼이 재생 버튼이면 시작 부분을 true로 설정하십시오. 그런 다음 버튼을 누르면 false로 설정합니다. 부울 값에 따라 버튼을 누를 때마다 이미지가 변경되어야합니다. 당신의 방법에서

AVAudioPlayer *theAudio; 

: .H 파일에서

theAudio 선언합니다