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;
}

   }

感谢帮助。

4

1 回答 1

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.
 }

此外,将 self 添加为 audioPlayerDidFinishPlaying:successfully: 的观察者,以便在音频播放器完成播放时重置。

希望它可以帮助你。

于 2013-02-09T16:57:43.020 回答