[英]In IOS core audio, how do you find the true current play head position of a file player audio unit?
我有一个程序,它使用文件播放器音频单元来播放,暂停和停止音频文件。 我实现这一点的方法是初始化文件播放器音频单元以在零位置播放文件,然后当用户按下暂停按钮时,我停止AUGraph,捕获当前位置,然后使用该位置作为起始位置当用户按下播放按钮时。 一切都按照应有的方式工作,但每经过3到4次暂停然后再播放,这首歌就会在我暂停的那一刻开始播放半秒到一秒。
我无法弄清楚为什么会发生这种情况,你们有什么想法吗? 这是我的代码的简化版本。
//initialize AUGraph and File player Audio unit
...
...
...
//Start AUGraph
...
...
...
// pause playback
- (void) pauseAUGraph {
//first stop the AuGrpah
result = AUGraphStop (processingGraph);
// get current play head position
AudioTimeStamp ts;
UInt32 size = sizeof(ts);
result = AudioUnitGetProperty(filePlayerUnit,
kAudioUnitProperty_CurrentPlayTime, kAudioUnitScope_Global, 0, &ts,
&size);
//save our play head position for use later
//must add it to itself to take care of multiple presses of the pause button
sampleFrameSavedPosition = sampleFrameSavedPosition + ts.mSampleTime;
//this stops the file player unit from playing
AudioUnitReset(filePlayerUnit, kAudioUnitScope_Global, 0);
NSLog (@"AudioUnitReset - stopped file player from playing");
//all done
}
// Stop playback
- (void) stopAUGraph {
// lets set the play head to zero, so that when we restart, we restart at the beginning of the file.
sampleFrameSavedPosition = 0;
//ok now that we saved the current pleayhead position, lets stop the AUGraph
result = AUGraphStop (processingGraph);
}
可能你应该使用数据包计数而不是时间戳,因为你只想暂停和播放音乐,而不是显示时间信息。
有关使用此方法的示例,请参阅BufferedAudioPlayer 。
这可能是由于代码的四舍五入问题:
例如,如果每次按下暂停按钮,您的计时器将在实际暂停时间之前的0.5 / 4秒记录,您仍会看到所需的结果。 但是在重复四次之后,你创造的空间量是0.5 / 4倍4,这是你似乎正在经历的半秒钟。
因此,我会特别注意您使用的对象类型,并确保它们不会不恰当地舍入。 尝试使用double float
样本时间来尝试缓解这个问题!
希望这是明确和有帮助的! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.