在IOS核心音频中,您如何找到文件播放器音频单元的真实当前播放头位置?

Bel*_*leg 6 iphone audio core-audio audiounit ios

我有一个程序,它使用文件播放器音频单元来播放,暂停和停止音频文件.我实现这一点的方法是初始化文件播放器音频单元以在零位置播放文件,然后当用户按下暂停按钮时,我停止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);
}
Run Code Online (Sandbox Code Playgroud)

way*_*way 0

这可能是由于代码的舍入问题造成的:

例如,如果每次按下暂停按钮时,计时器都会在实际暂停时间之前 0.5/4 秒进行记录,您仍然会看到所需的结果。但再重复四次后,您创建的空间量是 0.5/4 乘以 4,这就是您似乎正在经历的半秒。

因此,我会仔细注意您正在使用的对象类型,并确保它们不会不当舍入。尝试使用double float为您的样本时间来尝试缓解该问题!

希望这是清楚且有帮助的!:)