0

我正在尝试编写一个循环来检查每当音频 player.currenttime 增加 2 秒时它应该执行更新视图方法

- (void)myTimerMethod{

 NSLog(@"myTimerMethod is Called");

myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(checkPlaybackTime:)
                                         userInfo:nil
                                          repeats:YES];

  }


- (void)checkPlaybackTime:(NSTimer *)theTimer
  {
    float seconds =  audioplayer.currenttime;

    NSLog(@"Cur: %f",audioPlayer.currentTime ); 

    if (seconds = seconds + 2){

    [self update view];
}

 - (void)UpdateView{



if  (index < [textArray count])
 {
     self.textView.text = [self.textArray objectAtIndex:index];
   self.imageView.image = [self.imagesArray objectAtIndex:index];
   index++;
}else{

    index = 0;


   }
 }

如果音频 player.currenttimer 增加 2 秒,那么正确的写入方法是什么,然后执行此操作。

当前时间的 NSLog 始终显示 0.00。这是为什么。它应该随着音频播放器的播放而增加。

感谢帮助。

4

2 回答 2

1

首先,尝试在 NSLog 中使用浮点“秒”而不是当前时间。

NSLog(@"Cur: %f", seconds); 

当前时间不是浮点数,它是一个 NSTimer 对象,因此您必须在 NSLog 文本中使用 %@ 所以

NSLog(@"Cur: %@",audioPlayer.currentTime ); 

也应该工作。

假设您的 audioPlayer 设置正确,如果您正在寻找计时器何时为 2 秒,您的 if 语句将是

if(seconds == 2){
    [self update view];
}

if you're looking for each time the timer hits an even number, i.e. 2, 4, 6, etc. your if statement will be

if(seconds % 2 == 0){
    [self update view];
}

The % in an if statement is the modulo sign: http://www.cprogramming.com/tutorial/modulus.html

Also, your current if statement is assigning rather than checking the seconds variable. To check it, you need == not =. However, your current if statement will never be true since you're checking a variable by itself + 2. To put this another way, if seconds equals 2, your if statement is asking if 2 == (2+2) or if it it is 4, it's asking if 2 == (4+2). This statement cannot validate as true.

Hope this helps!

于 2013-03-03T03:58:52.093 回答
1

What i understood from your given explanation that you want to increment the time-interval something like this

Timer calls after 0.55
Timer calls after 0.60
Timer calls after 0.65
Timer calls after 0.70

& so on.

如果那是你想要做的。然后我认为你可以这样做,通过将repeats:YES 更改为repeats:NO 以便计时器不重复,然后在onTimer 中,只需启动一个间隔更长的新计时器。

您需要一个变量来保存您的时间间隔,以便您可以通过 onTimer 将其延长一点。

此外,您可能不再需要保留计时器,因为它只会触发一次,当它触发时,您将获得一个新计时器。

float gap = 0.50;

[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];

-(void) onTimer {
gap = gap + .05;
[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
}

希望这可以帮助你

于 2013-03-03T21:26:57.230 回答