我有一个使用文件播放器音频单元播放,暂停和停止音频文件的程序。我完成这个任务的方式是初始化文件播放器音频单元以在零位播放文件,然后当用户按下暂停按钮时,停止AUGraph,捕获当前位置,然后将该位置用作起始位置当用户按下播放按钮时。一切都按照它应该的方式工作,但是我每隔3到4次打一个停顿然后弹奏,这首歌开始播放一半到整整一秒,然后我停下来。在IOS核心音频中,如何找到文件播放器音频单元的当前播放头位置?
我不明白为什么会发生这种情况,你们有没有任何想法?这里是我的代码的简化版本。
//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);
}
音频文件是什么格式? – 2013-05-09 18:25:05
他们是m4a文件。 – Beleg 2013-05-09 21:40:22
好吧,我不是音频压缩方面的专家,但是文件往往会被压缩成块,而当您寻找它时,可能会到最近的块而不是实际的位置。如果我遇到任何更明确的问题,我会发布一个答案。 – 2013-05-10 07:35:13