Avkit play (avfoundation, avkit, audio and video, swift 4, with code)

Time:2020-10-18

Audio and video, simple, easy to use, avkit of course
For more flexible control, avfoundation will be used.


main points:

  • Use resources (usually videos, pictures and live photos in the photo library),
  • Play,
  • Editor,
  • Capture (taking pictures and recording videos)
  • Export resources (processed resources, photos, edited videos, exported to album)

Avfoundation, video loading and export, a large number of use of asynchronous.
Simply sending a message is definitely not going to work. The current thread is blocked and stuck for a long time.
Avfoundation is designed to make full use of 64 bit hardware and multithreading.


First, play,

Play local video files, and remote video and streaming media.

Local file, single play

Let’s talk about the avplayer viewcontroller in avkit
Avplayerviewcontroller is a subclass of viewcontroller,

Avplayerviewcontroller is very powerful on TV OS. (this article only introduces IOS platform)

There are many playback controls in avplayerviewcontroller.
In replay, local files can be played, suspended, fast forward and fast backward, and the length width ratio of the video can be adjusted (that is, the picture is moderate in the screen or covered with the screen).

Play video, Apple design is very simple, the code is as follows:

//Take a URL and create an avplayer instance
    Let player = avplayer (URL: "your URL")
    //Create another instance of avplayerviewcontroller
    let playerViewController = AVPlayerViewController()
    
    playerViewController.player = queuePlayer
    
    present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }//There's a closure here. It's going to play again.

Local files, multiple continuous playback

Play together, use avqueueplayer to put multiple videos in a video queue and play them successively
Avqueueplayer is a subclass of avplayer.
Play multiple resources in order.

Avplayeritem contains a lot of video resource information, including resource location URI, track information, video duration, etc.

According to Apple documents, avplayeritem is used to manage the timing and rendering status of resources played by players. He has an avasset property to play the resource.

var queue = [AVPlayerItem]()   
   let videoClip = AVPlayerItem(url: url)
   queue.append(videoClip)
    //The queue queue can continue to add avplayeritem instances
    let queuePlayer = AVQueuePlayer(items: queue)

    let playerViewController = AVPlayerViewController()
    playerViewController.player = queuePlayer
    
    present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }

Picture in picture function in iPad

The picture in picture feature in iPad supports background sound effects for avaudiosession,
stayAppdelegateOfdidFinishLaunchingWithOptionsAdd the following code in the background mode,
First of all, check the related background functions in Xcode’s target capability.

let session = AVAudioSession.sharedInstance()
    do {
        try session.setCategory(AVAudioSessionCategoryPlayback)
        try session.setActive(true)
    } catch let error {
        print("AVFoundation configuration error: \( error.localizedDescription )There is a problem with the AV configuration ")
    }
    //It is necessary to do so because of the video function of picture in picture, apple handles the background task.

Streaming media playing and network video playing

The local resource path URL is replaced by the network URL.

Optimize, exit the playback interface after playing

override func viewDidLoad() {
        super.viewDidLoad()
        //Add playback completed listening
        NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
    }

  //Execute exit interface control
   @objc func playerItemDidReachEnd(){
        self.presentedViewController?.dismiss(animated: true, completion: {})
    }

Code see: https://github.com/BoxDengJZ/…

More:

The second is to set the captured session and preview it.

Set up front and rear cameras, focus and exposure, take photos (still pictures)


Next, export the movie file.

How to set focus


There is also video synthesis, which combines multiple video clips into one video file.


Finally, it’s about adding graphic overlays and animations to the video.


Recommended resources:
Avfoundation programming guide Apple documentation

Video tutorial

Big guy blog, avplayer local, network video playback related