我的应用程序检测特定级别的音频,如上午10:00(开始时间)-10:30AM(结束时间)等。当前时间到达开始时间(表示当前时间也是上午10:00)如何调用音频检测过程和停止检测过程(结束时间)上午10:30。假设另一个间隔从上午10:15开始(startTime) - 10:45 AM(endTime)如何从上午10:30到上午10:45更新endTime。如何检查当前时间到达startTime和endTime.Please帮我紧急。谢谢这里我的代码是
ViewController.h
@interface ViewController : UIViewController
{
BOOL IsListening;
}
//会话数组是字典数组,包含程序的开始和结束时间。
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
}
答案 0 :(得分:2)
启动计时器&amp;在ios中停止计时器
start Timer
timer= [NSTimer scheduledTimerWithTimeInterval:60.0f
target: self
selector: @selector(methodFromTimer)
userInfo: nil
repeats: YES];
stop timer
[timer invalidate];
timer=nil;
答案 1 :(得分:0)