Add Playback Slider to AVPlayer. Example In Swift.

In this sample Swift code, you will learn how to add a playback slider to AVPlayer so that users can start playing music anywhere. The following example code relates to the following:

  • Create AVPlayer and AVPlayerItem,
  • Programmatically create a UIB button,
  • Create an AVPlayerLayer and add it as a subviewer,
  • The action buttons for pausing and playing music are controlled by a rotary knob,
  • Add UISlider as a subview,
  • UISlider button to change the value of the event,
  • Set the duration of the music file in seconds,
  • Set the maximum value of UISlider to the duration of the music file in seconds.

Play slideshow Example

import of the ViewController from the AVFoundation
class: UIViewController.

var player:AVPlayer?
var playerItem:AVPlayerItem ?
var playButton:UIB button ?

override viewDidLoad() {

Replace the viewWillAppear(_ animated : Bool) {

let url = URL(string:
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)

let playerLayer=AVPlayerLayer(player : player !)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)

playButton = UIB button (type : UIB button.ButtonType.system) if UIB button
leaves xPostion: CGFloat = 50
let yPostion: CGFloat = 100
let buttonWidth:CGFloat = 150
let buttonHeight:CGFloat = 45

playButton !.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
playButton !.backgroundColor = UIColor.lightGray
playButton !.setTitle(Play, for: UIControl.State.normal)
playButton !.tintColor =
//playButton !.addTarget(self, action: Selector(playButtonTapped:), for: .touchUpInside)
playButton !.addTarget(self, action: #selector(self.playButtonTapped(_:)), for: .touchUpInside)


// Add a playback cursor

let playbackSlider = UISlider(frame:CGRect(x:10, y:300, width:300, height:20))
playbackSlider.minimumValue = 0

period allowed: CMTime = playerItem.asset.duration
let seconds : Float64 = CMTimeGetSeconds (Duration)

playbackSlider.maximumValue = Float(seconds)
playbackSlider.isContinuous = true
playbackSlider.tintColor =

playbackSlider.addTarget (self, action: #selector(self.playbackSliderValueChanged(_:)), for: .valueChanged)
// playbackSlider.addTarget (self, action: playbackSliderValueChanged:, forControlEvents: .ValueChanged)



never mind the seconds: Int64 = Int64 (playbackSlider.value)
let targetTime: CMTime = CMTimeMake (value: seconds, time scale: 1)

player !.seek(to : targetTime)

if player !.rate == 0
player ?.play()

@objc function playButtonTapped(_ sender:UIB button)
if player ?.rate == 0
player !.play()
//playButton !.setImage(UIImage(named: player_control_pause_50px.png), forState : UIControlState.Normal)
playButton!setTitle(Pause, for: UIControl.State.Normal)
} else {
player !.pause()
//playButton !.setImage(UIImage(named: player_control_play_50px.png), forState : UIControlState.Normal)
playButton !.setTitle(Play, for: UIControl.State.Normal)


For more information on Swift code examples and tutorials, see the Swift code examples page on this site.

Written by Sergei Kargopolov

Related Tags:

 avplayer observe current timeavplayer swiftavplayer buffer progress swiftswift video scrubberavplayer observer swift 4avplayer stop playing swiftavplayer seek to time swiftavaudioplayer progress slideraddperiodictimeobserver example swiftavplayerviewcontroller exampleswift audio player with sliderswiftui avplayerswift slider

Leave a comment

Your email address will not be published. Required fields are marked *