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

UIKit
import of the ViewController from the AVFoundation
class: UIViewController.

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

override viewDidLoad() {
super.viewDidLoad()
}

Replace the viewWillAppear(_ animated : Bool) {
super.viewWillAppear(animated))

let url = URL(string: https://s3.amazonaws.com/kargopolov/kukushka.mp3)
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)
self.view.layer.addSublayer(playerLayer)

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 = UIColor.black
//playButton !.addTarget(self, action: Selector(playButtonTapped:), for: .touchUpInside)
playButton !.addTarget(self, action: #selector(self.playButtonTapped(_:)), for: .touchUpInside)

self.view.addSubview(playButton!)

// 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 = UIColor.green

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

}

@objc-function-displaySliderValueChanged(_displaySlider:UISlider)
{

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.

http://server.digimetriq.com/wp-content/uploads/2021/01/Determine-Users-Current-Location-Example-in-Swift.jpeg

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 *