AVPLayer从Firebase数据库中的URL播放视频

时间:2017-08-26 14:15:18

标签: ios database firebase avplayer

我有自动从Firebase数据库中提取文本的原型单元格。我想将视频合并到这里。我如何使用AVPlayer从FireBase数据库中的URL播放视频。我希望它尽可能自动化。

我不是一位经验丰富的开发人员,而且我一直在磕磕绊绊,所以请提供"宝贝等级"答案。

以下是从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!

}




}

这是AVPlayer代码:

//  VideoTestViewController.swift
//  Faces of the Holocaust Photographic History
//
//  Created by New User on 8/26/17.
//  Copyright © 2017 New User. All rights reserved.
//

import UIKit
import AVKit
import AVFoundation


class VideoTest: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
@IBAction func watchVideoDidClick(_ sender: Any) {

    playExternalVideo()
}

let videoURL = "SOMEVIDEOURL"

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()
    // Dispose of any resources that can be recreated.
}


}

如何合并这些内容以获取自动填充到单元格的视频。

编辑以下是修改后的代码

   //Code Beginning    
   import UIKit
   import Firebase
   import AVKit
   import AVFoundation

   struct postStruct {
let title : String!
let message : String!
let url : URL!
//let image : UIImage!

}

类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 url = snapshotValue?["url"] as? URL
        //let image = snapshotValue?["image"] as? UIImage

        self.posts.insert(postStruct(title: title, message: message, url: url) , 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!




}

func tableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    var selectedIndex = indexPath

    let url = URL(string: "url")
    let player = AVPlayer(url: url!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }

}

}

1 个答案:

答案 0 :(得分:0)

尝试将其显示在新屏幕上:

  1. 导入:导入AVKit,导入AVFoundation
  2. 然后当用户点击单元格时(在TableView的didSelectRow方法中)

    let videoURL = URL(string: "url")
    let player = AVPlayer(url: videoURL!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }
    
  3. 让它在单元格中发挥更难。正如您所评论的,这对您现在有用。

    编辑:在评论中回答您的问题: 这应该很容易。我认为你已经有了这个:

    self.posts.insert(postStruct(title: title, message: message), at: 0)
    

    这意味着您要将新的Post对象添加到posts数组中。然后重新加载数据。

    所以现在,我猜你的snapshotValue应该有一个网址,对吧?因为如果你说你需要播放视频,那么URL来自哪里?所以我的猜测是快照有这个网址。因此,您需要向Post对象添加新属性,即url。

    所以现在每个单元格都包含一个带有url的Post对象。当用户点击它时,你只需播放该帖子的网址。它清楚了吗?

    EDIT2: 这样做:

    let url = posts[indexPath.row].url

    EDIT3:

    1. 将您的第44行更改为:
    2. let urlString = snapshotValue?["url"] as? String

      let url = URL(string: urlString!)

      1. 将您的didSelect方法更改为: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)