如果我犯了任何错误,请告诉我,不要贬低这个问题。
这是我试图播放视频的问题,以下是代码
import UIKit
import AVFoundation
// custom UIView to display frame for avplayer
class VideoPlayerView: UIView {
let urlString String?
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .blackColor()
//static url for video
urlString = "https://firebasestorage.googleapis.com/v0/b/gameofchats-762ca.appspot.com/o/message_movies%2F12323439-9729-4941-BA07-2BAE970967C7.mov?alt=media&token=3e37a093-3bc8-410f-84d3-38332af9c726"
if let url = NSURL(string: urlString) {
let player = AVPlayer(URL: url)
let playerLayer = AVPlayerLayer(player: player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
player.play()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//查看控制器以启动视频播放器
class VideoPlayerController: UIViewController {
var message: Messages?{
didSet{
}
}
let overlayView: UIView = {
let view = UIView()
view.isUserInteractionEnabled = true
view.backgroundColor = UIColor(white: 0, alpha: 0.5)
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let height = view.frame.width * 9 / 16
let videoPlayerFrame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
let videoPlayerView = VideoContainerView(frame: videoPlayerFrame)
view.addSubview(videoPlayerView)
videoPlayerView.center = view.center
}
在VideoPlayerView的init方法中我已经使用了urlString属性的静态url视频,它正常播放。我想从我的videoPlayerController类设置urlString属性我怎么能这样做?因为当我初始化
let view = VideoPlayerView(frame:frame) // this code will call init method before i can set the urlString property.
任何人都知道如何做到这一点。请帮忙。提前感谢您的任何建议。
答案 0 :(得分:1)
只需使用所需参数创建其他初始化程序并调用它。
convenience init(frame: CGRect, urlString: String) {
self.init(frame: frame)
//make here what you want
}