In the main sound of iOS, how do you find the true current position of the head of the playback of the audio device of the file player?

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); } 
+6
source share
2 answers

Maybe you should use the number of packets instead of timestamps, because you just want to pause and play music, rather than displaying time information.

See BufferedAudioPlayer for an example of using this method.

+1
source

Perhaps this is due to rounding issues with your code:

For example, if every time you press the pause button, your timer will record 0.5 / 4 seconds before the actual pause time, you will still see the desired result. But after repeating for four times, the amount of space you created is 0.5 / 4 times 4, which is half the second that you seem to be experiencing.

Thus, I would pay close attention to the types of objects you are using and make sure that they are not rounded improperly. Try using double float for your sampling time to try to alleviate this problem!

Hope this is clear and helpful! :)

0
source

Source: https://habr.com/ru/post/921976/


All Articles