我有一個使用文件播放器音頻單元播放,暫停和停止音頻文件的程序。我完成這個任務的方式是初始化文件播放器音頻單元以在零位播放文件,然後當用戶按下暫停按鈕時,停止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