У меня есть программа, которая использует аудиоустройство проигрывателя файлов для воспроизведения, приостановки и остановки аудиофайла. Я достигаю этого путем инициализации аудиоустройства файлового проигрывателя для воспроизведения файла в нулевой позиции, а затем, когда пользователь нажимает кнопку паузы, я останавливаю 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);
}