Reputation: 97
I've got an audio player that plays audio retrieved from Core Date. Play & pause work fine. I'm trying to implement a 'Jump Forward 30 Seconds' button and seeking any pointers as to how I would go about that.
Code for my 'Play/pause' button
@IBAction func playPressed(sender: AnyObject) {
let play = UIImage(named: "play")
let pause = UIImage(named: "pause")
if audioPlayer.playing {
pauseAudioPlayer()
audioPlayer.playing ? "\(playButton.setImage( pause, forState: UIControlState.Normal))" : "\(playButton.setImage(play , forState: UIControlState.Normal))"
}else{
playAudio()
audioPlayer.playing ? "\(playButton.setImage( pause, forState: UIControlState.Normal))" : "\(playButton.setImage(play , forState: UIControlState.Normal))"
}
}
Upvotes: 3
Views: 3802
Reputation: 1849
in swift 3:
player.play()
player.currentTime=60
unit is second no need to stop at all
Upvotes: 4
Reputation:
Get the current time of the player and plus 30s to the current time.Then make the palyer play at the time (current time + 30).
var currentTime = player.currentTime
player.playAtTime(currentTime + 30.0)
If you want to use a slider to change the currentTime, you can use player.currentTime = TimeInterval(slider.value) * player.duration
to get it.
Upvotes: 1
Reputation: 51
For iOS we need to use CMTime. Only CMTime can be fed to the player in order to jump forward or backward from current position. Now current stream position if the player is running any stream can be had using currentTime property of the player.
Good Luck. Let me know if you have any further questions on this.
Upvotes: 2
Reputation: 89509
How about something like this?
@IBAction func skipForward30SecondsPressed(sender: AnyObject) {
var currentTime = audioPlayer.currentTime
audioPlayer.stop()
audioPlayer.playAtTime(currentTime + 30.0) // plus 30 seconds
}
Upvotes: 0