作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个视频选择 Controller ,这样用户就可以选择他想上传到 Firebase 的视频。现在我遇到的问题是我可以选择视频但无法在主视图中播放。它的工作方式应该有点像 Instagram 中的图像/视频选择器,如果是视频,它会自动播放。我根本不需要图片,我只需要上传视频。
我现在选择视频时看到的是第一帧的静止图像:
如果有人能告诉我需要在何处添加内容以自动播放此视频,甚至可以在屏幕中间添加一个播放按钮,我将不胜感激。
我现在显示选择的代码如下:
import UIKit
import Firebase
import Photos
private let reuseIdentifier = "SelectVideoCell"
private let headerIdentifier = "SelectVideoHeader"
class SelectVideoController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
// MARK: - Properties
var images = [UIImage]()
var assets = [PHAsset]()
var selectedImage: UIImage?
var header: SelectVideoHeader?
// MARK: - Init
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
fetchVideos()
//Register cell classes
collectionView.register(SelectVideoCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.register(SelectVideoHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
collectionView.backgroundColor = UIColor(red: 227/255, green: 227/255, blue: 227/255, alpha: 1)
}
// MARK: - UICollectionViewFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
let width = view.frame.width
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (view.frame.width - 3) / 4
return CGSize(width: width, height: width)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1
}
// MARK: - UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! SelectVideoHeader
self.header = header
if let selectedImage = self.selectedImage {
// Index selected image
if let index = self.images.index(of: selectedImage) {
// Asset associated with Selected Image
let selectedAsset = self.assets[index]
let videoManager = PHImageManager.default()
let targetSize = CGSize(width: 600, height: 600)
// Request Image
videoManager.requestImage(for: selectedAsset, targetSize: targetSize, contentMode: .default, options: nil) { (image, info) in
header.videoSelectView.image = image
}
}
}
return header
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SelectVideoCell
cell.videoSelectView.image = images[indexPath.row]
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.selectedImage = images[indexPath.row]
self.collectionView?.reloadData()
let indexPath = IndexPath(item: 0, section: 0)
collectionView.scrollToItem(at: indexPath, at: .bottom, animated: true)
}
@objc func handleDismiss() {
dismiss(animated: true, completion: nil)
}
@objc func handleNext() {
let uploadController = UploadVideoController()
uploadController.selectedImage = header?.videoSelectView.image
let navController = UINavigationController(rootViewController: uploadController)
present(navController, animated: true, completion: nil)
}
func configureUI() {
navigationController?.navigationBar.tintColor = .black
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = (UIImage())
//navigationItem.title = "Videos"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleDismiss))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(handleNext))
}
func getAssetFetchOptions() -> PHFetchOptions {
let options = PHFetchOptions()
// Fetch Limit
options.fetchLimit = 30
// Sort Videos by Date
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
// Set Sort Descriptor for Options
options.sortDescriptors = [sortDescriptor]
// Return Options
return options
}
func fetchVideos() {
let allVideos = PHAsset.fetchAssets(with: .video, options: getAssetFetchOptions())
print("Function running")
// Fetch Videos on Background Thread
DispatchQueue.global(qos: .background).async {
// Enumerate Objects
allVideos.enumerateObjects({ (asset, count, stop) in
print("Count is \(count)")
let videoManager = PHImageManager.default()
let targetSize = CGSize(width: 600, height: 600)
let options = PHImageRequestOptions()
options.isSynchronous = true
// Request Video representation for specific Asset
videoManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { (image, info) in
if let image = image {
// Append image to data source
self.images.append(image)
// Append asset to data source
self.assets.append(asset)
// Set selected Image
if self.selectedImage == nil {
self.selectedImage = image
}
// Reload collectionView with images once counr has completed
if count == allVideos.count - 1 {
// Reload collectionView on main thread
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
}
}
})
})
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
最佳答案
您可以通过创建 AVPlayer
和 AVPlayerLayer
的实例来播放视频。在您的选择 View Controller 中添加以下代码或创建一个新的 View Controller 并通过在其中传递视频 url 来呈现。
添加以下代码来播放视频。
import AVFoundation
var avPlayerLayer : AVPlayerLayer?
var avPlayer : AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// You need to pass your url here.
self.avPlayer = AVPlayer(url: url)
self.avPlayerLayer = AVPlayerLayer(player: self.avPlayer!)
self.avPlayerLayer!.frame = self.view!.bounds
self.view.layer.addSublayer(self.avPlayerLayer!)
self.avPlayer!.play()
}
此代码将在您选择后立即播放您的视频。您需要在 avPlayer
中传递 url。
希望对您有所帮助。
关于ios - 如何让视频在我的视频选择 Controller 中播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071732/
我是一名优秀的程序员,十分优秀!