Christian
Christian

Reputation: 1586

AVAudioPlayerNode - Get Player State?

In an iOS-project I am using the AVAudioPlayerNode in conjunction with the AVAudioEngine and an AVAudioUnitTimePitch. Everything works peachy. However, I was wondering if there is a way to figure out what the current player's state (e.g. isPlaying, isPaused) or at least the playback position is.

While AVAudioPlayer at least allows you to get the currentTime-parameter, I could not yet figure out how to get that information with AVAudioPlayerNode. I tried playing around with the nodeTimeForPlayerTime and playerTimeForNodeTime methods described in the swift documentation but I couldn't make any progress.

Any help would be highly appreciated.

Upvotes: 0

Views: 1552

Answers (1)

Daniel J
Daniel J

Reputation: 386

Since the AVAudioPlayerNode is designed as an audio stream, it doesn't necessarily keep track of the time within a particular file. However, the AVAudioPlayerNode does keep a running time of how long its been playing all audio. This timer doesn't reset with each file, in order to change it, you must explicitly tell it where you want to start counting from.

So to find the current time the player has been playing you must do the following:

player.lastRenderTime.sampleTime / file.fileFormat.sampleRate

Now in order to get the timer to reset after each file, you must explicitly reset the players current time. To do this use the player.playAtTime: function.

If you would like an example, check one out here: https://github.com/danielmj/AEAudioPlayer

Upvotes: 1

Related Questions