Swift encapsulates a video player VGPlayer

Keywords: github network Swift Programming


Banners.png

# Preface

Before learning Swift, I had always wanted to do a project. This time I decided to spend nearly a month of my free time encapsulating a video player based on AVPlayer.

# Source code

  • GitHub address: VGPlayer
  • What suggestions can be put forward, leave a message under the blog, code writing is also generally expected to teach God, improve programming technology, if you feel good, welcome to star.

# Demonstration


demo1.gif

demo2.gif

# Functions

  • It integrates the common gestures of video player, including clicking display control view, double-clicking pause, horizontal sliding fast forward and backward, vertical sliding brightness and volume adjustment.
  • Full screen play, adaptive mobile phone screen rotation direction.
  • Custom Control View

# Ideas for Realization


Flow chart. png

VGPlayer

VGPlayer is a playback function for AVPlayer encapsulation, and displayView draws the player's picture.
The following classes are mainly used:

  • AVURLAsset is a subclass of AVAsset that can be used to initialize network requests for local or network video addresses, or to capture pictures of each frame of video to achieve the function of sliding preview (which should be added to version iterations later).
  • AVPlayerItem is the management of video data played by AVPlayer. It records the Asset resources played by AVPlayer, provides the time or status of video, and so on.
  • AVPlayer controls data and views
  • AVPlayerLayer Drawing Video View

VGPlayer encapsulates AVPlayer's optional proxy methods for callers

// player delegate
    // play state
    func vgPlayer(_ player: VGPlayer, stateDidChange state: VGPlayerState)
    // playe Duration
    func vgPlayer(_ player: VGPlayer, playerDurationDidChange currentDuration: TimeInterval, totalDuration: TimeInterval)
    // buffer state
    func vgPlayer(_ player: VGPlayer, bufferStateDidChange state: VGPlayerBufferstate)
    // buffered Duration
    func vgPlayer(_ player: VGPlayer, bufferedDidChange bufferedDuration: TimeInterval, totalDuration: TimeInterval)
    // play error
    func vgPlayer(_ player: VGPlayer, playerFailed error: VGPlayerError)

VGPlayerView

  • VGPlayerView is responsible for the display of pictures, only as a display, while the drawing layer is provided by AVPlayerLayer, which can inherit the customization of such control views.
  • VGPlayerView encapsulates AVPlayerLayer to provide alternative proxy methods
// player view delegate
    /// fullscreen
    func vgPlayerView(_ playerView: VGPlayerView, willFullscreen fullscreen: Bool)
    /// close play view
    func vgPlayerView(didTappedClose playerView: VGPlayerView)
    /// displaye control
    func vgPlayerView(didDisplayControl playerView: VGPlayerView)

VGPlayerError

  • VGPlayerError A struct is used to play back when Error appears

# Detail adjustment

  • Realization of Backstage Playing
    Setting up Project

backgroundModes.png
// AppDelegate settings
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        do
        {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        }
        catch let error as NSError
        {
            print(error)
        }
        return true
    }

Setting the Background mode of VGPlayer

self.player.backgroundMode = .proceed
  • VGPlayerUtils provides methods for judging video types and some general methods
  • UIButton+VGPlayer Extension Button Click Range
  • Timer+VGPlayer Solves Timer's retain cycle Problem

# Reference

# Summary

  • Understanding the overall structure of AVPlayer, the complete idea of the playback process and some problems encountered.
  • Some pits in interactive details such as screen rotation details, button click range adjustment

Posted by - - NC - - on Sat, 22 Jun 2019 13:40:23 -0700