Ios 环球媒体播放器AV3
我有一个自定义媒体播放器,我希望在我的应用程序中普遍使用它;然而,我不希望继续从一页到另一页复制和粘贴代码,因为我需要这个视频播放器在多个领域工作。我将如何在一个文件中单独创建它,并能够在每个需要它的文件中调用它?这是严格针对iOS的 代码如下:Ios 环球媒体播放器AV3,ios,swift,swift3,avplayer,Ios,Swift,Swift3,Avplayer,我有一个自定义媒体播放器,我希望在我的应用程序中普遍使用它;然而,我不希望继续从一页到另一页复制和粘贴代码,因为我需要这个视频播放器在多个领域工作。我将如何在一个文件中单独创建它,并能够在每个需要它的文件中调用它?这是严格针对iOS的 代码如下: let avPlayer = AVPlayer() var avPlayerLayer: AVPlayerLayer! let playerButton = UIButton() var timeWatcher : AnyObject! var
let avPlayer = AVPlayer()
var avPlayerLayer: AVPlayerLayer!
let playerButton = UIButton()
var timeWatcher : AnyObject!
var timeRemainingLabel = UILabel()
let seekSlider = UISlider()
var playerRateBeforeSeek : Float = 0
var loadingIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
// An AVPlayerLayer is a CALayer instance to which the AVPlayer can
// direct its visual output. Without it, the user will see nothing.
avPlayerLayer = AVPlayerLayer(player: avPlayer)
view.layer.insertSublayer(avPlayerLayer, at: 0)
//this is for the button, we will do it through storyboard
view.addSubview(playerButton)
playerButton.addTarget(self, action: #selector(playerButtonTapped), for: .touchUpInside)
let url = NSURL(string: "https://content.jwplatform.com/manifests/vM7nH0Kl.m3u8")
let playerItem = AVPlayerItem(url: url! as URL)
avPlayer.replaceCurrentItem(with: playerItem)
//timer info
let timeInterval : CMTime = CMTimeMakeWithSeconds(1.0, 10)
timeWatcher = avPlayer.addPeriodicTimeObserver(forInterval: timeInterval, queue: DispatchQueue.main, using: { (elapsedTime:CMTime) in
// print("the time has now been:", CMTimeGetSeconds(elapsedTime))
self.observeTime(elapsedTime: elapsedTime)
self.timeRemainingLabel.textColor = .white
self.view.addSubview(self.timeRemainingLabel)
//buffer indicator
self.loadingIndicatorView.hidesWhenStopped = true
self.view.addSubview(self.loadingIndicatorView)
self.avPlayer.addObserver(self, forKeyPath: "currentItem.playbackLikelyToKeepUp", options: .new, context: &playbackLikelyToKeepUpContext)
}) as AnyObject!
}
//buffer
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context == &playbackLikelyToKeepUpContext {
if avPlayer.currentItem!.isPlaybackLikelyToKeepUp {
loadingIndicatorView.stopAnimating()
} else {
loadingIndicatorView.startAnimating()
}
}
}
deinit {
//timer
avPlayer.removeTimeObserver(timeWatcher)
//buffer
avPlayer.removeObserver(self, forKeyPath: "currentItem.playbackLikelyToKeepUp")
}
private func updateTimeLabel(elapsedTime: Float64, duration: Float64) {
let timeRemaining: Float64 = CMTimeGetSeconds(avPlayer.currentItem!.duration) - elapsedTime
timeRemainingLabel.text = String(format: "%02d:%02d", ((lround(timeRemaining) / 60) % 60), lround(timeRemaining) % 60)
}
private func observeTime(elapsedTime: CMTime) {
let duration = CMTimeGetSeconds(avPlayer.currentItem!.duration)
// if isFinite(duration)
// {
let elapsedTime = CMTimeGetSeconds(elapsedTime)
updateTimeLabel(elapsedTime: elapsedTime, duration: duration)
//}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadingIndicatorView.startAnimating() //animating the buffer
avPlayer.play() // Start the playback
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Layout subviews manually - replace w/ storyboard
avPlayerLayer.frame = view.bounds
playerButton.frame = view.bounds
//time remaining - replace w/ story board
let controlsHeight: CGFloat = 30
let controlsY: CGFloat = view.bounds.size.height - controlsHeight
timeRemainingLabel.frame = CGRect(x: 5, y: controlsY, width: 60, height: controlsHeight)
//buffer - replace w/ storyboard
loadingIndicatorView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
}
func playerButtonTapped(sender: UIButton) {
let playerIsPlayer = avPlayer.rate > 0
if playerIsPlayer {
avPlayer.pause()
} else {
avPlayer.play()
}
}
// Force the view into landscape mode (which is how most video media is consumed.
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscape
}
override var shouldAutorotate: Bool {
return true
}
将播放机移动到一个单独的类中,并在需要播放机时初始化该类。由于视频在自己的类中被分离,因此该类是否跟踪视频当前是否在媒体播放机中播放?或者我也需要为此设置一个追踪器;因为我不想有人同时播放两个视频。当你不需要的时候,你应该去现场直播。如果你想在同一个实例中通过应用程序在全球范围内使用它,你应该让它成为一个我以前从未听说过的单身者;我只是查了一下,它看起来很有希望;您是否有任何关于swift 3 singleton的资源/参考资料可供我查阅?因为单件行李与swift 2和swift 3在swift 3上有区别,所以这只是一条单行线。请看这里: