简体   繁体   English

如何在iOS中的特定时间启动和停止计时器

[英]How to start and stop timer at particular times in iOS

My app detect the audio at particular intervel like 10:00AM(startTime) -10:30AM(endTime),etc.Now when the current time reaches start time(means current time is also 10:00 AM) how to call audio detection process and stop detection process at (endTime)10:30 AM . 我的应用在特定时间间隔检测音频,例如10:00 AM(startTime)-10:30AM(endTime)等,现在当当前时间到达开始时间(意味着当前时间也是10:00 AM)时,如何调用音频检测过程并在(endTime)10:30 AM停止检测过程。 suppose another interval start at 10:15AM(startTime) - 10:45 AM(endTime) how to update endTime from 10:30AM to 10:45AM.how to check when current time reaches startTime and endTime.Please help me its urgent.Thanks here my code is 假设另一个时间间隔是从10:15 AM(startTime)-10:45 AM(endTime)开始,如何将endTime从10:30 AM更新为10:45 AM。如何检查当前时间何时到达startTime和endTime。请帮我解决这个问题。谢谢这是我的代码

ViewController.h

@interface ViewController : UIViewController

{

  BOOL IsListening;

}

//session array is array of dictionaries contains program start and end time. // session数组是字典的数组,包含程序的开始和结束时间。

   ViewController.m

   -(void)viewDidLoad
   {

     [super viewDidLoad];

     NSTimer* listener_timer = [NSTimer scheduledTimerWithTimeInterval: 10.0 target: self
                                                  selector: @selector(listeningHandler:) userInfo: nil repeats: YES];

    }

   -(void)listeningHandler:(NSTimer*)time
    {

       if (!IsListening)
       {
          double currentTime=[[NSDate date] timeIntervalSince1970];

          for (NSDictionary * session in SessionsArray)
          {
            double startTime=[[session objectForKey:@“startTime”] doubleValue];
            double endTime=[[session objectForKey:@“endTime”] doubleValue];

            if (currentTime > startTime && currentTime < endTime)
            {
             //start listening.

              IsListening =YES;


              /*

             //detection process code

             */                 
               NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:endTime];
               NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
                                                       interval:5
                                                         target:nil
                                                       selector:@selector(fireEndtimeAlarm)
                                                       userInfo:nil
                                                        repeats:YES];

              NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
             [runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

            }

          }

       }
      else
      {

         double currentTime=[[NSDate date] timeIntervalSince1970];
         for (NSDictionary * session in SessionsArray)
         {
            double startTime=[[session objectForKey:@"startTime"] doubleValue];
            double endTime=[[session objectForKey:@"endTime”] doubleValue];

            if (currentTime > startTime && currentTime < endTime)
            {
            // in between session

               IsListening =YES;
            /*

             //detection process code

             */          

             }

         }

       }
  }

 -(void)fireEndtimeAlarm

  {

     IsListening = NO;

     //stop detecting process at endTime  

  }

Start Timer & Stop Timer in ios 在iOS中启动计时器和停止计时器

start Timer
timer= [NSTimer scheduledTimerWithTimeInterval:60.0f
                                            target: self
                                          selector: @selector(methodFromTimer)
                                          userInfo: nil
                                           repeats: YES];

stop timer

[timer invalidate];
timer=nil;

You can achieve this using 2 Local notifications 您可以使用2条本地通知来实现

First of All schedule a specific notification at a specific time when time is reached then a trigger will run and at that time Start audio and 首先,在到达时间的特定时间安排特定的通知,然后将运行触发器,然后启动音频和

Schedule a second notification after 30 min when that will be fired then stop audio 安排第二个通知在30分钟后触发,然后停止播放音频

For local notifications you can follow these 对于本地通知,您可以按照以下说明进行操作

Link1 链接1

Link2 链接2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM