Audio - Jump Forward 30 Seconds

我只是一个虾纸丫 提交于 2019-12-10 10:38:35

问题


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

}

回答1:


in swift 3:

player.play()
player.currentTime=60

unit is second no need to stop at all




回答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.




回答3:


How about something like this?

@IBAction func skipForward30SecondsPressed(sender: AnyObject) {

    var currentTime = audioPlayer.currentTime

    audioPlayer.stop()

    audioPlayer.playAtTime(currentTime + 30.0) // plus 30 seconds
}



回答4:


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.



来源:https://stackoverflow.com/questions/28932558/audio-jump-forward-30-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!