【发布时间】:2012-07-31 17:27:12
【问题描述】:
我有一个程序,它使用文件播放器音频单元来播放、暂停和停止音频文件。我完成此操作的方法是初始化文件播放器音频单元以在零位置播放文件,然后当用户按下暂停按钮时,我停止 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);
}
【问题讨论】:
-
音频文件是什么格式的?
-
好吧,我不是音频压缩方面的专家,但文件往往被压缩成块,当你寻找它时,它可能会转到最近的块而不是实际位置。如果我遇到任何更明确的问题,我会发布答案。
-
有人解决了吗?我遇到了同样的问题。我正在使用 60 fps 的 displaylink 播放与音频单元播放的音频的 currentTime 同步的节拍器。我注意到 0.05 秒的延迟非常明显
-
嗨@Beleg你能告诉我如何在播放歌曲期间以秒为单位获取时间吗?即 0,1,2 .... 秒。我想在播放器中以秒为单位显示时间
标签: iphone ios audio core-audio audiounit