TheJeff
TheJeff

Reputation: 4131

AudioKit 5 iOS Current Playback Time

Hello fellow AudioKit users,

I'm trying to setup AudioKit 5 with a playback time indication, and am having trouble.

If I use AudioPlayer's duration property, this is the total time of the audio file, not the current playback time.

ex:

let duration = player.duration

Always gives the file's total time.

Looking at old code from AKAudioPlayer, it seemed to have a "currentTime" property.

The migration guide (https://github.com/AudioKit/AudioKit/blob/v5-main/docs/MigrationGuide.md) mentions some potentially helpful classes from the old version, however the "AKTimelineTap" has no replacement with no comments from the developers... nice.

Also still not sure how to manipulate the current playback time either...

I've also checked out Audio Kit 5's Cookbooks, however this is for adding effects and nodes, not necessarily for playback display, etc..

Thanks for any help with this new version of AudioKit.

Upvotes: 4

Views: 595

Answers (1)

RoySparrow
RoySparrow

Reputation: 41

You can find playerNode in AudioPlayer, it's AVAudioPlayerNode class.

Use lastRenderTime and playerTime, you can calculate current time.

ex:

// get playerNode in AudioPlayer.
let playerNode = player.playerNode

// get lastRenderTime, and transform to playerTime.
guard let lastRenderTime = playerNode.lastRenderTime else { return }
guard let playerTime = playerNode.playerTime(forNodeTime: lastRenderTime) else { return }
        
// use sampleRate and sampleTime to calculate current time in seconds.
let sampleRate = playerTime.sampleRate
let sampleTime = playerTime.sampleTime
let currentTime = Double(sampleTime) / sampleRate

Upvotes: 4

Related Questions