I have prototype cells that automatically pull text from a Firebase database. I want to incorporate videos into this as well. How would I use AVPlayer to play a video from a URL in the FireBase database. I want it to be as automatic as possible.
I am not an experienced developer and am stumbling through this, so please provide "baby level" answers.
Here is the code for the cells that pull from firebase.
//
// FirebaseVidoes.swift
// Faces of the Holocaust Photographic History
//
// Created by New User on 8/25/17.
// Copyright © 2017 New User. All rights reserved.
//
import UIKit
import Firebase
import AVKit
import AVFoundation
struct postStruct {
let title : String!
let message : String!
//let image : UIImage!
}
class FirebaseVidoes: UITableViewController {
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundView = UIImageView(image: UIImage(named: "polandBackgroundBlur.jpg"))
let databaseRef = Database.database().reference()
databaseRef.child("Posts").queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let snapshotValue = snapshot.value as? NSDictionary
let title = snapshotValue?["title"] as? String
let message = snapshotValue?["message"] as? String
//let image = snapshotValue?["image"] as? UIImage
self.posts.insert(postStruct(title: title, message: message) , at: 0)
self.tableView.reloadData()
})
//post()
// Do any additional setup after loading the view.
}
func post(){
let title = "Title"
let message = "Message"
let post : [String : AnyObject] = ["title": title as AnyObject,
"message": message as AnyObject]
let databaseRef = Database.database().reference()
databaseRef.child("Posts").childByAutoId().setValue(post)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
let label2 = cell?.viewWithTag(2) as! UILabel
label2.text = posts[indexPath.row].message
//let image1 = cell?.viewWithTag(3) as! UIImage
//image1.UIimage = posts[indexPath.row].image
return cell!
}
}And Here is the AVPlayer Code
/
/
/
/
/
/
/
import UIKit
import AVKit
import AVFoundation
class VideoTest: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/
}
@IBAction func watchVideoDidClick(_ sender: Any) {
playExternalVideo()
}
let videoURL = "
func playExternalVideo(){
let videoURL = NSURL(string: self.videoURL)!
let player = AVPlayer(url: videoURL as URL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
() -> Void in
playerViewController.player!.play()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
/
}
/
/
/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
/
/
}
*/
}How can I merge these to automatically populate the cell