我正在查看iOS SDK中的"Metronome"示例代码(http://developer.apple.com/library/ios/#samplecode/Metronome/Introduction/Intro.html).我正在以60 BPM的速度运行节拍器,这意味着每秒都会打勾.当我看一下外部手表(PC的手表)时,我看到节拍器运行速度太慢 - 它错过了每分钟一次,这就是app.15毫秒的一致错误.相关代码是:
- (void)startDriverTimer:(id)info {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Give the sound thread high priority to keep the timing steady.
[NSThread setThreadPriority:1.0];
BOOL continuePlaying = YES;
while (continuePlaying) { // Loop until cancelled.
// An autorelease pool to prevent the build-up of temporary objects.
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
[self playSound];
[self performSelectorOnMainThread:@selector(animateArmToOppositeExtreme) withObject:nil waitUntilDone:NO];
NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self.duration];
NSDate *currentTime = [[NSDate alloc] init];
// Wake up periodically …
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个应用程序,可以安排一些(大多数是微小的)音频文件的定时播放.问题:它需要定时准确的音乐.这些声音将不会播放频繁(它不是,例如采样器或鼓包),但他们将需要绝对精确地放置.这个问题分为两部分:
我该如何组织时间安排?我听说NSTimer
不准确,或者至少不可靠?我应该以某种方式使用MIDI吗?还是一个紧凑的循环和一些具有高分辨率的时间获取功能?
一旦我完成了计时,我知道何时播放我的声音......我该怎么玩它们?是否NSSound
快速可靠,足以保持所需的准确度?