2013-02-09 49 views
1

我想要將圖像視圖和文字瀏覽與音頻播放器綁定在一起。因此,當音頻播放器暫停時,應該暫停更新圖像瀏覽和文本瀏覽,並且當播放器恢復時,應該恢復更新圖像視圖和文本瀏覽。將圖像視圖和文字瀏覽與音頻播放器綁定索引

- (void)updateText:(NSTimer *)theTimer 
{ 
if (index < [myArray count]) 
{ 
    self.textView.text = [self.myArray objectAtIndex:index]; 
    self.imageView.image = [self.imagesArray objectAtIndex:index]; 
    index++; 
} 
else { 
    index = 0; 
} 

    } 

感謝您的幫助。

回答

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:成功:當audioplayer播放完畢通知重置。

希望它可以幫助你。

+0

讓我試試這個,如果它的工作。 – user1452248 2013-02-09 16:58:58

相關問題