How to set a variable that represents a time in the future in absolute terms Objective-C(如何设置一个以绝对值表示未来时间的变量Objective-C)
问题描述
动机:我正在开发一个应用程序,它可以让多个(客户端)手机从(服务器)手机读取音频数据.这个想法是他们必须所有在确切同时一起播放歌曲.
Motivation: I'm working on an app that makes several (client) phones read audio data from a (server) phone. The idea is that they must all play the song at the exact same time together.
前提:我必须想办法让所有手机都在某个绝对的时间戳开始(即它与任何一部手机的使用设置时钟等无关)..基于一些 研究 我认为最好的方法是使用 CFAbsoluteTimeGetCurrent();
这里的想法是我得到服务器与每部手机通信所需的延迟(b/c GKSession 显然是串行而不是并行完成的),将该延迟添加到当前时间,然后让每部手机从那个开始时间.这个开始时间必须是绝对的,不能是相对的.
Premise: I must figure out a way to make all phones start at a certain time stamp that is absolute (ie it's not relative to the use set clock of either phone etc..).. based on some research I figured the best way to do this is to use CFAbsoluteTimeGetCurrent();
The idea here is that I get the latency it takes the server to communicate with each phone (b/c GKSession is done serially not in parallel apparently), add that latency to the current time, and then have every phone play the song starting at that start time. This start time must be absolute, it cannot be relative.
问题:我怎样才能用数字表示未来的某个时间,以便日后用于构建 CFDateRef.(它必须以数字表示的原因是我必须能够以数据包的形式发送它..)
Question: How can I represent a time in the future in a number, that can be later on used to construct a CFDateRef. (The reason it has to be expressed as a number is that I must be able to send it in a packet..)
示例:这段代码解释了我想要实现的目标:
Example: This is a bit of code that explains what I'm trying to achieve:
-(void)viewDidLoad
{
_timer = [Timer new];
[_timer setCurrentTimeAsReferencepoint];
[self performSelector:@selector(performAction) withObject:NULL afterDelay:5];
}
-(void)performAction
{
double diff = [_timer getTimeElapsedinAbsTime];
double timeInFuture = diff + [Timer getCurTime];
NSLog(@"this is time in future in abs terms %f",timeInFuture);
CFDateRef futureDate = CFDateCreate(NULL, timeInFuture);
CFDateRef dateNow = CFDateCreate(NULL, [Timer getCurTime]);
Boolean keepTesting = true;
while (keepTesting) {
if (CFDateCompare(dateNow, futureDate,NULL) == 0) // ie both times are equal
{
NSLog(@"now is the time!");
keepTesting = false;
} else {
NSLog(@"now isn't the time.. skip");
}
}
}
在 Timer.m 中:
in Timer.m:
-(void)setCurrentTimeAsReferencepoint
{
_referencePoint = [[self class] getCurTime];
NSLog(@"this is reference point %f",_referencePoint);
}
+(double)getCurTime
{
return (double)CFAbsoluteTimeGetCurrent();
}
// not used in the above code.. but used when i compare the time of the server phone with the client phone
+(double)getTimeDifference:(double)time1
time2:(double)time2
{
CFDateRef newDate = CFDateCreate(NULL, time2);
CFDateRef oldDate = CFDateCreate(NULL, time1);
CFTimeInterval difference = CFDateGetTimeIntervalSinceDate(newDate, oldDate);
NSLog(@"this is time difference %f",fabs(difference));
CFRelease(oldDate); CFRelease(newDate);
// fabs = absolute value
return fabs(difference);
}
推荐答案
事实证明比我想象的要简单(注意:getCurTime 是在问题中定义的):
It turns out it's simpler than I thought (note: getCurTime is defined in the question):
-(void)performAction
{
double curTime = [Timer getCurTime];
double timeInFuture = 2 + curTime;
NSLog(@"this is time in future in abs terms %f and this is curTime %f",timeInFuture, curTime);
CFDateRef futureDate = CFDateCreate(NULL, timeInFuture);
CFDateRef dateNow = CFDateCreate(NULL, curTime);
Boolean keepTesting = true;
while (keepTesting) {
dateNow = CFDateCreate(NULL, [Timer getCurTime]);
if (CFDateCompare(dateNow, futureDate,NULL) >= 0) // ie both times are equal or we've
// gone past the future date time
{
NSLog(@"now is the time!");
keepTesting = false;
return;
} else {
NSLog(@"now isn't the time.. skip");
}
}
}
这篇关于如何设置一个以绝对值表示未来时间的变量Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何设置一个以绝对值表示未来时间的变量Objective-C
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01