Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何检查Audioplayer的播放时间增加2秒_Objective C_Xcode_Ios6_Avaudioplayer_Nstimer - Fatal编程技术网

Objective c 如何检查Audioplayer的播放时间增加2秒

Objective c 如何检查Audioplayer的播放时间增加2秒,objective-c,xcode,ios6,avaudioplayer,nstimer,Objective C,Xcode,Ios6,Avaudioplayer,Nstimer,我试图写一个循环来检查每当audioplayer.currenttime增加2秒时,它应该执行updateview方法 - (void)myTimerMethod{ NSLog(@"myTimerMethod is Called"); myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self

我试图写一个循环来检查每当audioplayer.currenttime增加2秒时,它应该执行updateview方法

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


   }
 }
-(void)myTimerMethod{
NSLog(@“myTimerMethod被称为”);
myTimer=[NSTimer scheduledTimerWithTimeInterval:1.0
目标:自我
选择器:@selector(检查播放时间:)
用户信息:无
重复:是];
}
-(无效)检查播放时间:(n计时器*)计时器
{
浮动秒数=audioplayer.currenttime;
NSLog(@“Cur:%f”,audioPlayer.currentTime);
如果(秒=秒+2){
[自我更新视图];
}
-(void)UpdateView{
如果(索引<[textArray count])
{
self.textView.text=[self.textArray objectAtIndex:index];
self.imageView.image=[self.imagesArray objectAtIndex:index];
索引++;
}否则{
指数=0;
}
}
如果audio player.currenttimer增加2秒,正确的写入方法是什么

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


感谢您的帮助。

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

NSLog(@"Cur: %f", seconds); 
当前时间不是浮点,它是NSTimer对象,因此您必须在NSLog文本中使用%@以便

NSLog(@"Cur: %@",audioPlayer.currentTime ); 
也应该有效

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

if(seconds == 2){
    [self update view];
}
如果您正在查找计时器每次命中偶数,即2、4、6等,则您的if语句将为

if(seconds % 2 == 0){
    [self update view];
}
if语句中的%是模符号:

另外,您当前的if语句是赋值而不是检查seconds变量。要检查它,您需要==not=。但是,您当前的if语句永远不会为true,因为您正在检查一个变量本身+2。换句话说,如果seconds等于2,您的if语句将询问if 2==(2+2)或者如果它是4,则询问2==(4+2)。此语句不能验证为true


希望这能有所帮助!

我从您给出的解释中了解到,您希望像这样增加时间间隔

Timer calls after 0.55
Timer calls after 0.60
Timer calls after 0.65
Timer calls after 0.70
&等等

如果这就是你想要做的。那么我认为你可以这样做,通过将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];
}

希望这对您有所帮助

感谢您的详细回复。非常感谢。感谢您的回复。此解决方案是我所寻找的最佳解决方案。