user1268135
user1268135

Reputation: 57

currentTime, deviceCurrentTime, playAtTime for AVAudioPlayer

I am working on AVAudioPlayer but I'm confused about

Can anybody explain with some sample code?

Upvotes: 1

Views: 1818

Answers (2)

gnasher729
gnasher729

Reputation: 52530

Once an AVAudioPlayer has been initialised, "duration" is the length of the sound or music file in seconds.

"currentTime" is a position in the music file in seconds from the start. You can read currentTime while the music is playing and it will tell how far the music has been played. You can set currentTime at any time while the sound is playing, and it will immediately play from the changed time. For example to skip 10 seconds, just write

myAudioPlayer.currentTime += 10.0;

You can set currentTime while the audio player is paused or not started, and that changes the position in the music where the next "play" command will be playing.

"currentDeviceTime" is something entirely different and it is used together with "playAtTime" to let you start playing exactly when you want, or to start two sounds simultaneously.

Best to imagine that the speakers of your phone have a built-in clock which might return a different time then your normal clock. "currentDeviceTime" returns the current time according to the clock of the speaker. If you call

[myAudioPlayer startPlayAt:myAudioPlayer.currentDeviceTime + 10.0];

a "play" command will be started in exactly ten seconds from now. currentDeviceTime has nothing to do with your music file, it's just a clock. Heaven knows why Apple hasn't just used for example dispatch_time_t which should have worked just fine.

Upvotes: 4

Kapil Choubisa
Kapil Choubisa

Reputation: 5232

currentTime is property to set player's current time. So player will start playing your audio from that time.

deviceCurrentTime is property to get time for player is playing or paused. If you have two players playing or paused (not stopped) so device time will be increase till both of player get stop. If any one is playing or paused device time will increase.

playAtTime is method to Plays a sound asynchronously, starting at a specified point in the audio output device’s timeline.

For more discussion on this see Apple Document for AVAudioPlayer you will get better idea on all these three.

Upvotes: 2

Related Questions