我有一个使用文件播放器音频单元来播放、暂停和停止音频文件的程序。我完成此操作的方法是初始化文件播放器音频单元以在零位置播放文件,然后当用户按下暂停按钮时,我停止 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);
}