30天学习Swift项目实战第三天--------本地视频播放器

前端之家收集整理的这篇文章主要介绍了30天学习Swift项目实战第三天--------本地视频播放器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

和之前一样,创建一个新的项目。 再次巩固UITableView的使用。 在Build 里面的Copy Bundle Resources里面添加资源文件 直接上代码: // // ViewController.swift // PlayVideo // // Created by luopan on 16/8/4. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit //导入视频播放的库 import AVKit import AVFoundation

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

//展示数据的tableView
@IBOutlet weak var videoTableView: UITableView!
//显示的数据
var data = [
    video(image: "videoScreenshot01",title: "Introduce 3DS Mario",source: "Youtube - 06:32"),video(image: "videoScreenshot02",title: "Emoji Among Us",source: "Vimeo - 3:34"),video(image: "videoScreenshot03",title: "Seals Documentary",source: "Vine - 00:06"),video(image: "videoScreenshot04",title: "Adventure Time",source: "Youtube - 02:39"),video(image: "videoScreenshot05",title: "Facebook HQ",source: "Facebook - 10:20"),video(image: "videoScreenshot06",title: "Lijiang Lugu Lake",source: "Allen - 20:30")
]
//调用系统自带播放器的变量
var playViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidLoad() {
    super.viewDidLoad()
    
    //为自身设置代理
    videoTableView.dataSource = self
    videoTableView.delegate = self
}
//点击播放
[@IBAction](http://my.oschina.net/u/866341) func playVideoButtonDidTouch(sender: UIButton) {
    //调用系统播放器
    let path = NSBundle.mainBundle().pathForResource("emoji zone",ofType: "mp4")
    playerView = AVPlayer(URL: NSURL(fileURLWithPath: path!))
    playViewController.player = playerView
    self.presentViewController(playViewController,animated: true) {
        self.playViewController.player?.play()
    }
}
//每一行的高度
func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 220
}
//分成两列
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}
//每一列的行数
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return data.count
}
//每一行的现实风格
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //解析每一行的cell,并且强制类型转换为 VideoCell
    let cell = videoTableView.dequeueReusableCellWithIdentifier("VideoCell",forIndexPath: indexPath) as! VideoCell
    //由于在这里使用的是自定义的cell类,所以需要强制类型转换
    //解析得到数据
    let video = data[indexPath.row]
    
    //为每一个控件指定数据
    cell.videoScreenshot.image = UIImage(named: video.image)
    cell.videoTitleLabel.text = video.title
    cell.videoSourceLabel.text = video.source
    
    return cell
}

}

还有一个和UITableViewCell绑定的swift文件,如下: // // VideoCellTableViewCell.swift // PlayVideo // // Created by luopan on 16/8/4. // Copyright © 2016年 Hust University. All rights reserved. //

import UIKit

struct video { let image: String let title: String let source: String }

class VideoCell: UITableViewCell {

@IBOutlet weak var videoScreenshot: UIImageView!

@IBOutlet weak var videoTitleLabel: UILabel!

@IBOutlet weak var videoSourceLabel: UILabel!



override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool,animated: Bool) {
    super.setSelected(selected,animated: animated)

    // Configure the view for the selected state
}

}

原文链接:https://www.f2er.com/swift/323249.html

猜你在找的Swift相关文章