Support >
  About independent server >
  How do I set AVPlayer to play online videos and local files
How do I set AVPlayer to play online videos and local files
Time : 2024-10-14 14:09:04
Edit : Jtti

AVPlayer as a powerful media playback framework, can play local and network video, this article Warner cloud for everyone to share how to play online video and local files through AVPlayer, including the database to read the video file Settings.

/uploads/images/202410/14/072c0d4e756e0839470475407480e7ff.jpg  

If you need to play an online video, first provide the URL of the online video, and use AVPlayerItem and AVPlayer to load and play the video. The main steps are: The premise of using AVPlayer is to import the AVFoundation framework in the project, so you need to import the AVFoundation framework first. To create an AVPlayerItem, create an instance of AVPlayerItem with the URL of the video:

let videoURL = URL(string: "https://example.com/video.mp4")! let playerItem = AVPlayerItem(url: videoURL)

To create AVPlayer, use the AVPlayerItem instance to create an AVPlayer object:

let player = AVPlayer(playerItem: playerItem)

Call AVPlayer's play method to start playing the video:

player.play()

Add AVPlayerLayer to display video; If you need to display a video in a custom UIView, you can use AVPlayerLayer:

let playerLayer = AVPlayerLayer(player: player)

playerLayer.frame = view.bounds

view.layer.addSublayer(playerLayer)

To play a local video, for example, read a video file from the local database, perform the following steps: Read the path of the video file from the database and query the path of the video file from the database as required.

To create an AVPlayerItem, create an instance of AVPlayerItem using the path to the local video file.

let localVideoPath = "path/to/your/local/video.mp4"let localVideoURL  = URL(fileURLWithPath: localVideoPath)let playerItem = AVPlayerItem(url: localVideoURL)

To create AVPlayer, use the AVPlayerItem instance to create an AVPlayer object.

let player = AVPlayer(playerItem: playerItem)

To play the video, call the play method of AVPlayer to start playing the video.

player.play()

Add AVPlayerLayer to display the video. If you want to customize the video to be displayed in UIView, use AVPlayerLayer.

let playerLayer = AVPlayerLayer(player: player)

playerLayer.frame = view.bounds

view.layer.addSublayer(playerLayer)

In addition, AVPlayer provides a variety of ways to monitor playback status, such as monitoring playback progress, error handling, and so on. Add listeners to AVPlayer to respond to these events.

// Listen for playback status changes

player.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 600), queue: nil) { [weak self] time in

self? .updatePlaybackTime(time: time)}

// Listening playback error

player.replaceCurrentItem(with: playerItem)

playerItem.addObserver(self, forKeyPath: "status", options: .new, context: nil)

The above steps are about playing online videos and local files with AVPlayer. Whether it is reading local files from a network URL or a database, AVPlayer provides powerful support to make video playback simple and efficient

JTTI-Defl
JTTI-COCO
JTTI-Selina
JTTI-Ellis
JTTI-Eom