【发布时间】:2015-11-11 12:10:59
【问题描述】:
我正在 Swift 2 中构建一个视频播放器应用程序,它将加载 3 个视频,这些视频的数据(标题、描述、缩略图和视频 url)保存在名为 videos 的全局数组中。
此应用包含一个包含 3 个单元格的表格视图,当用户单击其中一个单元格时,视频播放器视图将打开。我将其配置为具有可以播放 youtube 视频的 Web 视图。我知道我可以使用 YouTube API 以更好的方式做到这一点,但现在我只专注于使用这 3 个视频。
我创建了一个名为 PlayerViewController.swift 的新 Cocoa Touch Class 文件,并在其中配置了视频播放器。
Main.storyboard 中的 Video Player View 的类设置为 PlayerViewController。
标签工作正常,但 WebView 无法加载,我可以移动它,但根本没有视频。 我究竟做错了什么?我必须为 Web 视图设置任何类吗?如何使视频在此 WebView 中正确播放? 代码在下面。
提前致谢!
播放器视图控制器:
//import Foundation
import UIKit
//import WebKit
//import AVFoundation
class PlayerViewController: UIViewController {
@IBOutlet weak var videoView: UIWebView!
@IBOutlet weak var playerTitle: UILabel!
@IBOutlet weak var playerDescription: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
videoView.allowsInlineMediaPlayback = true
videoView.loadHTMLString("<iframe width=\"\(videoView.frame.width)\" height=\"\(videoView.frame.height)\" src=\"\(videos[activeVideo]["video"])?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)
playerTitle.text = videos[activeVideo]["title"]
playerDescription.text = videos[activeVideo]["description"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
视图控制器:
import UIKit
var videos = [Dictionary<String,String>()]
var activeVideo = -1
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
if videos.count == 1 {
videos.removeAtIndex(0)
// Check if places != to 1 and not 0 because we created the dictionary places as a global var outside the ViewController and that can not be empty.
videos.append(["title":"Video 1","description":"Description 1","thumbnail":"http://img.youtube.com/vi/6oXTFb-52Zc/0.jpg","video":"https://www.youtube.com/embed/6oXTFb-52Zc"])
videos.append(["title":"Video 2","description":"Description 2","thumbnail":"http://img.youtube.com/vi/dkwuCeCQVg8/0.jpg","video":"https://www.youtube.com/embed/dkwuCeCQVg8"])
videos.append(["title":"Video 3","description":"Description 3","thumbnail":"http://img.youtube.com/vi/6Zf_70xM1Cw/0.jpg","video":"https://www.youtube.com/embed/v=6Zf_70xM1Cw"])
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return videos.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
let url = NSURL(string: videos[indexPath.row]["thumbnail"]!)
let data = NSData(contentsOfURL: url!)
cell.img?.image = UIImage(data: data!)
cell.ttl?.text = videos[indexPath.row]["title"]
cell.dsc?.text = videos[indexPath.row]["description"]
return cell
}
func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
activeVideo = indexPath.row
return indexPath
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "back" {
activeVideo = -1
// Resetting the value of activeVideo
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
您是否遇到任何控制台错误?
-
没有错误...只是一个移动的空 WebView :(
-
我看到在您的播放器视图控制器中,在“videoView.loadHTMLString”上设置的字符串未正确转义。如果那在您的实际代码中,您将遇到问题。 This-> ideo]["video"])?&playsinline
-
我尝试过这样做,但出现了更多错误...我认为由于 ["video"] 已经在 () 中,因此不需要转义。
-
*斜线括号内
标签: ios objective-c xcode swift webview