2012-03-02 31 views
1

餘米這個問題掙扎時,這麼長時間,但沒能找到這個如何恢復更新視圖恢復AVAudioplayer

任何解決方案,我有這個問題是,當我停下,我想從暫停意見audioplayer更新,所以我用定時器無效暫停加載視圖,但當我恢復播放時,只有Audioplayer恢復不是視圖導致我無效的計時器。

現在我怎麼可以從視圖暫停的那一點恢復更新視圖。通過再次啓動計時器,我可以再次開始更新視圖,但程序會如何知道它在哪個視圖中暫停,並且應該從該點恢復更新視圖。有多個視圖涉及顯示代碼只有兩個。

-(void)playpauseAction:(id)sender 
{ 
    if 

    ([audioPlayer isPlaying]){ 

[sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected]; 

[audioPlayer pause]; 

[timer invalidate]; 

    } else { 

[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal]; 

[audioPlayer play]; 

self.timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:@selector(displayviewsAction:) userInfo:nil repeats:NO]; 
    } 

} 

- (void)displayviewsAction:(id)sender 

FirstViewController *viewController = [[FirstViewController alloc] init]; 

viewController.view.frame = CGRectMake(0, 0, 320, 480); 

[self.view addSubview:viewController.view]; 

[self.view addSubview:toolbar]; 

[viewController release]; 

self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:@selector(secondViewController) userInfo:nil repeats:NO]; 

-(void)secondViewController { 
SecondViewController *secondController = [[SecondViewController alloc] init]; 

secondController.view.frame = CGRectMake(0, 0, 320, 480); 

[self.view addSubview:secondController.view]; 

[self.view addSubview:toolbar]; 

[secondController release]; 

self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:@selector(ThirdviewController) userInfo:nil repeats:NO]; 
} 

非常感謝您的回答,並幫助我解決這個問題。

回答

1

使用後臺線程的觀點基本上綁定到的球員,這樣的:

UIImageView* imageView; //the view that gets updated 

int current_selected_image_index = 0; 
float time_for_next_image = 0.0; 

AVAudioPlayer* audioPlayer; 

NSArray* image_times; //an array of float values representing each 
         //time when the view should update 

NSArray* image_names; //an array of the image file names 

-(void)update_view 
{ 
    UIImage* next_image_to_play = [image_names objectAtIndex:current_selected_image_index]; 
    imageView.image = next_image_to_play; 
} 

-(void)bind_view_to_audioplayer 
{ 
    while(audioPlayer.isPlaying) 
    { 
     float currentPlayingTime = (float)audioPlayer.currentTime; 
     if(currentPlayingTime >= time_for_next_image) 
     { 
      current_selected_image_index++; 
      [self performSelectorOnMainThread:@selector(update_view) withObject:nil waitUntilDone:NO]; 
      time_for_next_image = [image_times objectAtIndex:[current_selected_image_index+1)]; 
     } 
     [NSThread sleep:0.2]; 
    } 
} 

-(void)init_audio 
{ 
current_selected_image_index = 0; 
time_for_next_image = [image_times objectAtIndex:1]; 
} 

-(void)play_audio 
{ 
    [audioPlayer play]; 
    [self performSelectorInBackground:@selector(bind_view_to_audioplayer) withObject:nil]; 
} 

-(void)pause_audio 
{ 
    [audioPlayer pause]; 
    //that's all, the background thread exits because audioPlayer.isPlaying == NO 
    //the value of current_selected_image_index stays where it is, so [self play_audio] picks up 
    //where it left off. 
} 

此外,添加自己作爲觀察員audioPlayerDidFinishPlaying:successfully:通知時,audioplayer播放完畢復位。