如果其他人正在处理类似的问题,这花了我几个小时在谷歌上搜索,这就是我发现的如何专门检索和设置播放头。
从 AUFilePlayer 单元获取播放头:
AudioTimeStamp timestamp;
UInt32 size = sizeof(timestamp);
err = AudioUnitGetProperty(unit, kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, ×tamp, &size);
timestamp.mSampleTime
是该文件的当前播放头。转换mSampleTime
为浮点数或双精度数并除以文件的采样率以转换为秒。
为了重新启动 AUFilePlayer 的播放头,我有一个更复杂的场景,其中多个 AUFilePlayer 通过一个混音器,可以在不同的时间、多次和不同的循环次数进行调度。这是一个真实的场景,让它们在正确的时间重新启动需要一点代码。
每个 AUFilePlayer 有四种场景及其时间表:
播放头在开头,所以可以正常安排。
播放头已超过项目的持续时间,根本不需要安排。
播放头在项目开始之前,所以开始时间可以上移。
播放头正在播放一个项目,因此需要调整文件内播放的区域,并且需要单独安排剩余的循环(以便它们完整播放)。
这是一些演示这一点的代码(一些外部结构来自我自己的代码,而不是 Core Audio,但机制应该很清楚):
// Add each region
for(int iItem = 0; iItem < schedule.items.count; iItem++) {
AEFileScheduleItem *scheduleItem = [schedule.items objectAtIndex:iItem];
// Setup the region
ScheduledAudioFileRegion region;
[file setRegion:®ion schedule:scheduleItem];
// Compute where we are at in it
float playheadTime = schedule.playhead / file.sampleRate;
float endOfItem = scheduleItem.startTime + (file.duration*(1+scheduleItem.loopCount));
// There are four scenarios:
// 1. The playhead is -1
// In this case, we're all done
if(schedule.playhead == -1) {
}
// 2. The playhead is past the item start time and duration*loopCount
// In this case, just ignore it and move on
else if(playheadTime > endOfItem) {
continue;
}
// 3. The playhead is less than or equal to the start time
// In this case, simply subtract the playhead from the start time
else if(playheadTime <= scheduleItem.startTime) {
region.mTimeStamp.mSampleTime -= schedule.playhead;
}
// 4. The playhead is in the middle of the file duration*loopCount
// In this case, set the start time to zero, adjust the loopCount
// startFrame and framesToPlay
else {
// First remove the start time
region.mStartFrame = 0;
double offsetTime = playheadTime - scheduleItem.startTime;
// Next, take out any expired loops
int loopsExpired = floor(offsetTime/file.duration);
int fullLoops = region.mLoopCount - loopsExpired;
region.mLoopCount = 0;
offsetTime -= (loopsExpired * file.duration);
// Then, adjust this segment of a loop accordingly
region.mStartFrame = offsetTime * file.sampleRate;
region.mFramesToPlay = region.mFramesToPlay - region.mStartFrame;
// Finally, schedule any remaining loops separately
if(fullLoops > 0) {
ScheduledAudioFileRegion loops;
[file setRegion:&loops schedule:scheduleItem];
loops.mStartFrame = region.mFramesToPlay;
loops.mLoopCount = fullLoops-1;
if(![super.errors check:AudioUnitSetProperty(unit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, ®ion, sizeof(region))
location:@"AudioUnitSetProperty(ScheduledFileRegion)"])
return false;
}
}
// Set the region
if(![super.errors check:AudioUnitSetProperty(unit, kAudioUnitProperty_ScheduledFileRegion, kAudioUnitScope_Global, 0, ®ion, sizeof(region))
location:@"AudioUnitSetProperty(ScheduledFileRegion)"])
return false;
}