Я пытаюсь воспроизвести видео из библиотеки фотографий с помощью AVPlayer, но по какой-то причине не могу заставить его работать. Я пытался прочитать все вопросы по этому поводу, но ни один из предложенных ответов, похоже, не работает.
Мой код неправильный или мне нужно сделать что-то еще, чтобы заставить его работать? Включить в возможностях или что-то подобное?
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let videoPickerController = UIImagePickerController()
// MARK:- UI Elements
let SelectVideoBtn : UIButton = {
let a = UIButton(type: .roundedRect)
a.translatesAutoresizingMaskIntoConstraints = false
a.setTitle("Select Video", for: .normal)
a.backgroundColor = UIColor.systemOrange
a.layer.cornerRadius = 10
a.addTarget(self, action: #selector(SelectAVideo(sender:)), for: .touchUpInside)
return a
}()
let MasterPreviewView : UIView = {
let a = UIView()
a.translatesAutoresizingMaskIntoConstraints = false
a.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
a.layer.cornerRadius = 10
return a
}()
let HorizontalToolStack : UIStackView = {
let a = UIStackView()
a.translatesAutoresizingMaskIntoConstraints = false
a.axis = .horizontal
a.distribution = .equalSpacing
a.alignment = .fill
a.spacing = 20
return a
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
// Do any additional setup after loading the view.
// delegates and data stuff
videoPickerController.delegate = self
// Selection Button
self.view.addSubview(SelectVideoBtn)
SelectVideoBtn.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 30).isActive = true
SelectVideoBtn.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 100).isActive = true
SelectVideoBtn.widthAnchor.constraint(equalToConstant: 200).isActive = true
SelectVideoBtn.heightAnchor.constraint(equalToConstant: 50).isActive = true
// Master Preview View
self.view.addSubview(MasterPreviewView)
MasterPreviewView.topAnchor.constraint(equalTo: SelectVideoBtn.bottomAnchor, constant: 30).isActive = true
MasterPreviewView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
MasterPreviewView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true
MasterPreviewView.heightAnchor.constraint(equalToConstant: 400).isActive = true
MasterPreviewView.isUserInteractionEnabled = false
let videoURL = NSURL(string: "assets-library://asset/asset.MP4?id=A0FBB444-15D7-4ED2-9EB2-922E4FB11F7A&ext=MP4")
PlayVideo(inView: MasterPreviewView, pathToVideo: videoURL!)
}
internal func VideoPicker(){
videoPickerController.sourceType = .photoLibrary
videoPickerController.mediaTypes = ["public.movie"]
videoPickerController.allowsEditing = true
present(videoPickerController, animated: true, completion: nil)
}
// MARK:- Delegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let videoURL = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerReferenceURL")] as? NSURL
print(videoURL!)
videoPickerController.dismiss(animated: true, completion: nil)
// Now Play the Selected Video
PlayVideo(inView: MasterPreviewView, pathToVideo: videoURL!)
}
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("Selection Cancelled")
self.dismiss(animated: true, completion: nil)
}
// MARK:- Button Selectors
@objc func SelectAVideo(sender : UIButton){
VideoPicker()
}
//MARK:- Custom Functions
/**
A function to play a video in the View of our choice.
- Parameter inView : Specify the view in which youo want the video player to play your video
- Parameter pathToVide : The path of the video you want to play
*/
private func PlayVideo(inView parentView : UIView, pathToVideo urlPath : NSURL) {
// Downcasting the NSURL to URL
let player = AVPlayer(url: urlPath as URL)
let playerController = AVPlayerViewController()
playerController.player = player
let playerLayerAV = AVPlayerLayer(player: player)
//now we set the size of frame to be like the view ("backview")
playerLayerAV.frame = parentView.bounds
parentView.layer.addSublayer(playerLayerAV)
player.play()
player.rate = 0.5
}
}