2012-01-02 3 views
1

내 앞의 코드는 위의 코드 일시 정지 버튼에서AVAudioPlayer - 일시 정지 버튼

- (void) playaudio: (id) sender 
{ 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme" 
               ofType:@"mp3"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 

    self.audioPlayer = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:fileURL error:nil]; 

    self.audioPlayer.currentTime = 0; 
    [self.audioPlayer play]; 
} 

- (void)pause: (id)sender 

{ 
    [audioPlayer pause]; 

} 

- (void)stop: (id)sender 

{ 

    [audioPlayer stop]; 

} 

는이 오디오 파일을 다시 시작했는데 정지 버튼이 아닌 일시 정지로 작동 하였다.

이제는 내 코드에 간단한 문장을 추가했는데 어느 정도는 작동하지만 아직 내 기대치에는 미치지 못했습니다.

오디오 파일을 재생하고 일시 중지 단추를 클릭해도 아무 일도 일어나지 않지만 중지 단추를 클릭하면 오디오 파일 재생이 중지되고 일시 중지 단추를 누르면 오디오 파일이 다시 시작됩니다 정지 버튼을 눌러 정지합니다. 중지 버튼을 누른 다음 일시 중지 버튼 기능을 사용하지만 그 이전에는 사용하지 않는 이유는 무엇입니까? 나는 왜 이것을 얻지 못하니?

어떤 아이디어를 사람이 왜 이런 일이 있는지 어떤 아이디어가 있다면이

- (void)pause: (id)sender 

{ 

    [audioPlayer pause];  
    [audioPlayer prepareToPlay]; 
    [audioPlayer play]; 


} 


- (void) stop: (id) sender 

{ 
    [audioPlayer stop]; 

} 

일어나는 이유. 도와 주셔서 감사합니다.

미리 감사드립니다.

답변

4

오디오 파일을 재생할 때마다 오디오 파일을 재생성하지 마십시오. 여기에 당신이 할 수있는 방법은 다음과 같습니다

- (void) playaudio: (id) sender 
{ 
    if(self.audioPlayer == nil) { 
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme" 
               ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 

     self.audioPlayer = [[AVAudioPlayer alloc] 
       initWithContentsOfURL:fileURL error:nil]; 

     self.audioPlayer.currentTime = 0; //this could be outside the if if you want it to start over when they hit play 
    } 
    [self.audioPlayer play]; 
} 

- (void)pause: (id)sender 

{ 
    if([audioPlayer isPlaying]){ 
     [audioPlayer pause]; 
    } else { 
     [audioPlayer play]; 
    } 

} 

- (void)stop: (id)sender 

{ 

    [audioPlayer stop]; 

} 
+0

빙고를 .. + 1 .. 코딩 즐기 .. :) –

관련 문제