3

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))"
    }

}
J. Dorais
  • 97
  • 1
  • 11
  • Have you seen this post? https://stackoverflow.com/a/38658769/464016 – Lepidopteron Oct 17 '17 at 13:05
  • Possible duplicate of [AVPlayer seekToTime does not play at correct position](https://stackoverflow.com/questions/11462843/avplayer-seektotime-does-not-play-at-correct-position) – Lepidopteron Oct 17 '17 at 13:05

4 Answers4

4

in swift 3:

player.play()
player.currentTime=60

unit is second no need to stop at all

MartianMartian
  • 1,753
  • 1
  • 18
  • 26
  • 2
    This is the only correct answer here. `player.play(atTime: ...)` delays the playback by a certain time. to skip forward 30 seconds, simply do `player.currentTime += 30` – Luka Kerr Mar 01 '18 at 05:07
2

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.

learner
  • 51
  • 1
  • 6
1

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.

0

How about something like this?

@IBAction func skipForward30SecondsPressed(sender: AnyObject) {

    var currentTime = audioPlayer.currentTime

    audioPlayer.stop()

    audioPlayer.playAtTime(currentTime + 30.0) // plus 30 seconds
}
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215