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://my.oschina.net/asdfgzxcvb/blog/727576

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值