I have a program that uses the audio device of a file player to play, pause and stop the audio file. The way I do this is to initialize the audio device of the file player to play the file at the zero position, and then when the user presses the pause button, I stop AUGraph, fix the current position and then use this position as the initial position when the user presses the play button . Everything works as it should, but every 3 or 4 times I pause and then play, the song starts playing half to a full second BEFORE the point where I pause.
I canβt understand why this is happening, any of you have any thoughts? here is a simplified version of my code.
//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); }
source share