SA-Plug-AVPlayer 0.1.0

SA-Plug-AVPlayer 0.1.0

paresh 维护。



  • Solution Analyst

SA-Plug-AVPlayer

简单且功能丰富的视频播放器,拥有丰富的自定义可能性和对 AVPlayer 的完全控制。根据您的需求调整设计和功能。

入门指南

这里我们有一个简单的插件,开发人员可以使用它来在 Swift 4.2 中集成为视频播放。PlugAVPlayer 允许您创建自己的 UI并添加用于播放和全屏的按钮等图片。

功能

  • 支持本地和网络播放
  • 支持具有扩展名的 URL 和嵌入视频 URL。(对于嵌入视频没有控制,它将使用 WkWebView 进行播放)
  • 仅支持 YouTube 嵌入 URL
  • 支持全屏
  • 支持带有缓冲加载器的自定义滑动条。
  • 支持快进和快退
  • 支持改变播放速率。
  • PlayerEventDelegate 代理方法可以控制下一个/上一个和重放视频,可以控制单个或多个 URL。
  • 支持本地保存视频。
  • 支持自定义播放器视图。
  • 支持字幕
  • 注意内存管理。

参考:滑动条来自 BufferSlider 的 GitHub 项目。

先决条件

Swift 4.2

iOS 10*

Xcode 10.2

安装

如果您不希望使用上述任何依赖项管理器,您可以将SA-Plug-AVPlayer手动集成到项目中。只需将源文件复制到您的项目目录中

示例

	import PlusAVPlayer

 	@IBOutlet weak var viewController: UIView!
    	@IBOutlet weak var viewVideo: ViewVideo!
	@IBOutlet weak var activityIndicator: UIActivityIndicatorView!

	override func viewDidLoad() {
    		super.viewDidLoad()  
 		DispatchQueue.main.async {
	        //Play video locally
        		//self.setUpPlayerWithLocal()
    		    //Play video with url
            self.setUpPlayerWithURlStreaming()
		}
	}
	//Play Video with url streaming
	func setUpPlayerWithURlStreaming()
    	 {
        //MARK : if url is emded. It will play in webview and managed automatically in webview
        viewVideo.configure(url: url,ControllView: self.viewOverlay,loader: self.activityIndicator)
        viewVideo.saveVideoLocally = true
        viewVideo.delegate = self
        viewVideo.currentVideoID = self.videoID
        viewVideo.play()
        viewVideo.activityIndicator?.startAnimating()
    }

	//Play Video locally
	func setUpPlayerWithLocal()
    {
        viewVideo.configure(ControllView: self.viewOverlay,loader: self.activityIndicator,localPath:self.arrlocalVideo[self.index],fileextension : "mp4")
        viewVideo.delegate = self
        viewVideo.currentVideoID = self.videoID
        viewVideo.play()
        viewVideo.activityIndicator?.startAnimating()
    }

	//With PlayerEventDelegate Method manage as you need iterate over array of urls and manage Next Previous End of video, fullscreen, totalTime and  many more.

	class ViewController : PlayerEventDelegate{

    func totalTime(_ player: AVPlayer) {
        
    }
    
    func AVPlayer(didEndPlaying: AVPlayer?) {
        //Play your next video
    }
    
    func AVPlayer(didTap overLay: AVPlayer?) {
        self.dropdown?.isHidden = true
    }
    
    func AVPlayer(willExpand player: AVPlayer?) {
        UIView.animate(withDuration: 0.3, animations: {
            self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
            self.viewVideo.isMiniMized = false
            self.btnClose.alpha = 0

            let value = UIInterfaceOrientation.landscapeLeft.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
        })
    }
    
    func AVPlayer(didTaptoNextvideo: AVPlayer?) {
        //Replace your video with next one
        if index != self.arrlocalVideo.count - 1
        {
            self.index += 1
            self.viewVideo.replacelocalVideo(path: self.arrlocalVideo[self.index], videoextension: "mp4")
            if index == self.arrVideos.count - 1{
                self.viewVideo.btnForward?.isEnabled = false
            }
            else
            {
                self.viewVideo.btnForward?.isEnabled = true
                self.viewVideo.btnBackward?.isEnabled = true
            }
        }
        
        //self.viewVideo.replacelocalVideo(path: "filename", videoextension: "mp4")
    }
    
    func AVPlayer(didTaptoPreviousvideo: AVPlayer?) {
        //Replace your video with previous one
        if index != 0
        {
            self.index -= 1
            self.viewVideo.replacelocalVideo(path: self.arrlocalVideo[self.index], videoextension: "mp4")
            if index == 0{
                self.viewVideo.btnBackward?.isEnabled = false
            }
            else
            {
                self.viewVideo.btnForward?.isEnabled = true
                self.viewVideo.btnBackward?.isEnabled = true
                
            }
        }
    }
}

快进和快退

我们在ViewController文件中给出了平移手势配置,请在您的viewvideo上添加手势识别器,您将能够手动访问快进和快退方法。我们保留了这段代码在开发者端,因为还有一个与之相关的拖拽视频视图功能。您可以利用管理多个案件的优势。

	if (touchPoint.x - initialTouchPoint.x) > 1{
       	        self.viewVideo.fastForwardPlayer()
       	}
	else if (initialTouchPoint.x - touchPoint.x) > 10{
		self.viewVideo.fastBackward()
        }

播放器速率

	self.viewVideo.changePlayerRate(rate: rate)

有一个方法可以改变播放器的速率

UI 规范

为视频播放器创建一个视图,以及包含所有控制按钮的另一个视图,例如:

  • 使用VideoControllButton子类用于播放、暂停和全屏
  • 使用VideoControllLabel子类用于时间标签和总时间
  • 使用BufferSlider用于UISlider,可让您自定义颜色和其他属性。

注意

请务必写出ButtonControlType:对于按钮 –> playpause, expand, forward, backward 对于标签 –> LabelControllType 1用于timelabelupdate 或 2用于总时间 在演示中查看更多细节。

直接将IB拖到UIViewController,16:9屏幕宽高比的约束(优先级为750,低于1000行),代码部分只需实现。在演示中查看更多细节。

viewVideo.configure(url: url,ControllView: self.viewController,loader: self.activityIndicator)

控制类型

	enum ButtonControlType :String {
        case PlayPause = "playpause"
        case Expand = "expand"
        case forward = "forward"
        case backward = "backward"
    	}

子类控制

User BufferSlider用于缓冲进度,VideoControllButton用于播放/暂停和全屏退出按钮,VideoControllLabel用于时间标签。

	VideoControllButton
	VideoControllLabel
	BufferSlider

基于

  • AVKit框架
  • 使用URLSession

作者

Solution Analyst