2012-03-13 2 views
1

내 프로젝트에서 노래를 재생하는 AVAudioPlayer가 있습니다. 일시 중지하고 다시 재생하면 일시 중지 한 위치에서 재생하는 대신 다시 재생됩니다. 왜 그런 일이 일어나는거야? 또한 처음으로 재생을 누르면 노래가 약 3-4 초 동안로드됩니다. 나는 그것을 다른 스레드에서로드하여 해결할 수 있다고 생각했지만 여전히 너무 느린로드 (적어도 뷰는 더 이상 고정되지 않음). 어떻게 해결할 수 있습니까? 코드는 다음과 같습니다.AVAudioPlayer 일시 중지 문제

- (IBAction)play:(id)sender { 
    songIsCurrentlyPaused = NO; 
    if(songIsCurrentlyPaused==YES){ 
    [self.background play]; 
    } else { 
    playQueue = dispatch_queue_create("volume_change", NULL); 
    dispatch_async(playQueue, ^{ NSString *filePath = 
     [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
     self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
     self.background.delegate = self; 
     [self.background setNumberOfLoops:1]; 
     [self.background setVolume:0.5]; 
     [self.background play]; }); 

     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 
    } 
} 

- (IBAction)pause:(id)sender { 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 

고마워요!

답변

1

당신은 play:

의 시작 부분에 NOsongIsCurrentlyPaused을 설정하는 것은을 주석보십시오 :

- (IBAction)play:(id)sender { 
    //songIsCurrentlyPaused = NO; 
    if(songIsCurrentlyPaused==YES){ 
    [self.background play]; 
    } else { 
    playQueue = dispatch_queue_create("volume_change", NULL); 
    dispatch_async(playQueue, ^{ NSString *filePath = 
     [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
     NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
     self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
     self.background.delegate = self; 
     [self.background setNumberOfLoops:1]; 
     [self.background setVolume:0.5]; 
     [self.background play]; }); 

     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 
    } 
} 

- (IBAction)pause:(id)sender { 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 

당신이거야 그 초기 일시 정지를 제거하려면 완전히 재구성 한 플레이어 설정. 사용자가 재생 버튼을 누르기 전에 초기화하고 노래를 미리 불러올 때도

[self.background prepareToPlay]; 

으로 전화하십시오. 또한 songIsCurrentlyPaused = NO; 코드의 일부 이전 위치로 이동하십시오.

편집 :

는 당신이 어딘가에 같은 loadView 또는 viweDidLoad에 초기화 코드를 이동해야합니다 초기 지연을 제거합니다. 당신이 백그라운드 스레드에서 데이터 를 사전로드 고려 wan't 수 있도록

//initialization code 
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"]; 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
    self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
    self.background.delegate = self; 
    [self.background setNumberOfLoops:1]; 
    [self.background setVolume:0.5]; 
    [self.background prepareToPlay]; 

지금이 UI 화면에서 지연의 원인이 될 수 있습니다.

귀하의 IBAction 방법은 수정해야 다음 일시 중지와

- (IBAction)play:(id)sender 
{ 

    if (songIsCurrentlyPaused==YES) 
    { 
     [self.background play]; 
    } 
    else 
    { 
     playQueue = dispatch_queue_create("volume_change", NULL); 
     dispatch_async(playQueue, ^{ 
     [self.background setCurrentTime: 0.0]; 
     [self.background play]; 
     }); 

     [self.progressBar setProgress:0.0 animated:YES]; 
     [trackNameLabel setText:@"Currently playing :\n some_song"]; 
     self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES]; 

    } 
    songIsCurrentlyPaused = NO; 
} 

- (IBAction)pause:(id)sender 
{ 
    songIsCurrentlyPaused = YES; 
    [self.background pause]; 
    [trackNameLabel setText:@"Currently playing : some_song (paused)"]; 
    [self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES]; 
} 
+0

문제가 해결! 고마워, 많이 감사! 하지만 로딩은 여전히 ​​느리다 : ( – nemesis

+0

@ 네메시스 : 나는 나의 대답을 편집했다. 내가 지금 내 맥이 아니기 때문에 코드를 읽을 수 없다. 작동해야하지만 약간의 조정이 필요할 수있다. 일부 백그라운드 스레드에서 미리로드를 이동할 수 있다면 (거의) 지연이 없어야합니다. –

+0

보기가로드 된 후 재생 버튼을 누르면 작동합니다. 완전히 제거 할 수있는 방법이 없습니까? 지연? 고마워요 !! – nemesis

관련 문제