0

動機:私は、複数の(クライアント)電話に(サーバー)電話からオーディオデータを読み取らせるアプリに取り組んでいます。アイデアは、彼ら全員がまったく同時に曲を演奏しなければならないということです。

前提:すべての電話を絶対的な特定のタイムスタンプで開始する方法を見つけなければなりません(つまり、どちらの電話の使用設定クロックなどとは関係ありません)。いくつかの調査に基づいて、私は次の最善の方法を考え出しました。ここCFAbsoluteTimeGetCurrent();での考え方は、サーバーが各電話と通信するのにかかる遅延を取得し(b / c GKSessionは明らかに並列ではなく直列に実行される)、その遅延を現在の時刻に追加してから、すべての電話を使用することです。その開始時間から曲を再生します。この開始時間は絶対値である必要があり、相対値にすることはできません。

質問:後でCFDateRefを作成するために使用できる、将来の時間を数値で表すにはどうすればよいですか。(数字で表現する必要があるのは、パケットで送信できる必要があるためです。)

例:これは、私が達成しようとしていることを説明するコードです。

-(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で:

-(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);    
}
4

1 に答える 1

0

思ったよりも簡単であることがわかりました(注:getCurTimeは質問で定義されています):

-(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");
        }
    }
}
于 2012-10-08T06:31:07.630 に答える