Designing a Custom Video Player in Ios with Avplayer

Creating a custom video player in iOS can significantly enhance the user experience by allowing for tailored controls and features. Using AVPlayer, Apple’s powerful media framework, developers can design a flexible and engaging video playback interface.

Understanding AVPlayer

AVPlayer is part of the AVFoundation framework and provides a simple API to play, pause, and control media content. It supports various media formats and allows for advanced features like buffering, seeking, and observing playback status.

Setting Up the Basic Video Player

To start, import AVFoundation and set up an AVPlayer instance with a media URL. Embed the AVPlayerLayer into your view to display the video content.

import AVFoundation
import UIKit

class VideoPlayerViewController: UIViewController {
    var player: AVPlayer!
    var playerLayer: AVPlayerLayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://example.com/video.mp4")!
        player = AVPlayer(url: url)
        playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = view.bounds
        view.layer.addSublayer(playerLayer)

        // Add play button or controls here
    }
}

Designing Custom Controls

Custom controls enhance user interaction. Common features include play/pause buttons, a progress slider, and volume controls. These can be added as UIButtons and UISliders, linked to AVPlayer actions.

let playPauseButton = UIButton(type: .system)
playPauseButton.setTitle("Play", for: .normal)
playPauseButton.addTarget(self, action: #selector(togglePlayPause), for: .touchUpInside)

@objc func togglePlayPause() {
    if player.timeControlStatus == .playing {
        player.pause()
        playPauseButton.setTitle("Play", for: .normal)
    } else {
        player.play()
        playPauseButton.setTitle("Pause", for: .normal)
    }
}

Adding a Progress Slider

The UISlider can be used to show and control playback position. Use a periodic time observer to update the slider as the video plays.

let timeObserver = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: 1), queue: .main) { [weak self] time in
    guard let self = self else { return }
    let currentTime = CMTimeGetSeconds(time)
    let duration = CMTimeGetSeconds(self.player.currentItem!.duration)
    self.slider.value = Float(currentTime / duration)
}

@objc func sliderValueChanged(_ sender: UISlider) {
    let duration = CMTimeGetSeconds(player.currentItem!.duration)
    let newTime = CMTime(seconds: Double(sender.value) * duration, preferredTimescale: 600)
    player.seek(to: newTime)
}

Handling Full-Screen Mode and Orientation

For an immersive experience, enable full-screen playback and handle device orientation changes. Use UIViewController’s supportedInterfaceOrientations and AVPlayerViewController for built-in full-screen support.

Conclusion

Designing a custom video player with AVPlayer allows for a tailored media experience on iOS. By combining AVFoundation’s capabilities with custom UI controls, developers can create engaging and user-friendly video applications.