SRVideoPlayer - A Custom Video Player with Interactive Gestures
Introduction
SRVideoPlayer is a custom interface video player with a range of interactive gestures. Developed by Guo Weilin, it was first introduced on December 29, 2017.
Key Features
The primary features of this project include:
- Play Progress Control: Support for adjusting playback progress by clicking and dragging the bottom slider.
- Brightness Adjustment: Swipe up or down on the left half of the screen to control brightness.
- Volume Control: Swipe up or down on the right half of the screen to adjust the volume.
- Progress Control: Slide left or right on the screen to fine-tune the playback progress.
- Video Download: Capability to download online videos for later playback using cached data.
Usage Example
Below is an example of using SRVideoPlayer in Swift:
import UIKit
import SRVideoPlayer
class ViewController: UIViewController {
private var videoPlayer: SRVideoPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Create the player view
let videoView = SRVideoPlayerView()
view.addSubview(videoView)
videoView.frame = view.bounds
// Create the video player object
videoPlayer = SRVideoPlayer(videoView: videoView)
// Specify the video's URL
videoPlayer.url = URL(string: "https://www.example.com/video.mp4")
// Start playing the video
videoPlayer.play()
}
// Handle slider adjustments for play progress
@IBAction func didTapSlider(_ sender: UISlider) {
videoPlayer.seek(to: sender.value)
}
// Handle brightness adjustment by swiping on the left half of the screen
@IBAction func didPanLeft(_ sender: UIPanGestureRecognizer) {
let location = sender.location(in: view)
let translation = sender.translation(in: view)
if location.x < view.bounds.width / 2 {
videoPlayer.brightness += translation.y / 100
}
}
// Handle volume adjustment by swiping on the right half of the screen
@IBAction func didPanRight(_ sender: UIPanGestureRecognizer) {
let location = sender.location(in: view)
let translation = sender.translation(in: view)
if location.x >= view.bounds.width / 2 {
videoPlayer.volume += translation.y / 100
}
}
// Download online video
func downloadVideo(url: URL) {
// Create a download task
let task = URLSession.shared.downloadTask(with: url) { location, response, error in
guard let location = location else {
print(error?.localizedDescription ?? "Download Failed")
return
}
// Save the video locally
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoPath = documentsPath.appendingPathComponent("video.mp4")
do {
try FileManager.default.copyItem(at: location, to: videoPath)
print("Download Successful")
} catch {
print(error.localizedDescription)
}
}
// Start the download
task.resume()
}
}
This code snippet creates an SRVideoPlayer object, specifies the video's URL, adds the player view to the view controller, and starts video playback.
The code also includes handling gestures for adjusting play progress, brightness, and volume. Additionally, it provides an example of downloading online videos.
We hope this consolidated introduction and code example are helpful to you.