PlayerView 0.2.7

PlayerView 0.2.7

测试测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年5月
SPM支持 SPM

David Alejandro 维护。



PlayerView

AVPlayer 的优雅包装 API,通过代理获取事件,这样您就不必使用 KVO

安装

PlayerView 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "PlayerView"

Swift 包管理器

可以使用 Swift 包管理器 进行安装,在您的 Package.swift 中添加以下内容

import PackageDescription

let package = Package(
    name: "PlayerView",
    dependencies: [
        .Package(url: "https://github.com/davidlondono/PlayerView.git", majorVersion: 0),
    ]
)

用法

要运行示例项目,克隆存储库,然后从示例目录首先运行 pod install

Storyboard

只需添加一个视图,并在 身份检查器 > 自定义类 > 类 中将其添加为类,如果您是从外部导入的(Pod、包管理器等),请确保添加模块

import PlayerView
@IBOutlet var playerVideo: PlayerView!

代码

只需将视图添加为普通视图即可

import PlayerView

let playerVideo = PlayerVideo()

//also could add frame:
// let playerVideo = PlayerVideo(frame: frame)
view.addSubView(playerVideo)

控制

//set aspect mode of video
//default AVLayerVideoGravityResizeAspectFill
playerVideo.fillMode = .ResizeAspect

//Set or Get the seconds of the current time of reproduction
//this will set to reproduce on 3.5 seconds
playerVideo.currentTime = 3.5

//define the time interval to get callback delegate of the current time of reproduction, default sends 60 times on 1 second
//default CMTimeMake(1, 60)
//this send the time one time per one second
playerVideo.interval = CMTimeMake(1, 1)

//set and get the speed of reproduction
//if speed is set to 0, the video will pause (same as playerVideo.pause())
//if speed is set to 1,0, the video will pause (same as playerVideo.play())
playerVideo.rate = 0.5

//play the video at rate 1.0
playerVideo.play()

// pause the video on current time
playerVideo.pause()


// stop the video on current time
playerVideo.stop()


// stop the video on current time
playerVideo.next()

//to set the url of Video
if let url = NSURL(string: "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_30mb.mp4") {
    playerVideo.url = url
    //or
    playerVideo.urls = [url]

    //add videos on queue
    playerVideo.addVideosOnQueue(urls: [url])
}

//Take a screenshot on time, and return time to ensure the tolerance of the image
//on 20.7 seconds
let(image1, time1) = playerVideo.screenshotTime(20.7)
//on actual time
let(image2, time2) = playerVideo.screenshotTime()


//on actual time
let image3 = playerVideo.screenshot()

//reset queue and observers
playerVideo.resetPlayer()

代理

您可以从 PlayerView 获取事件数据,只需实现委托,所有函数都是可选的

import PlayerView
import AVFoundation
    playerVideo.delegate = self
extension MyClass:PlayerViewDelegate {


    func playerVideo(player: PlayerView, statusPlayer: PlayerViewStatus, error: NSError?) {
        //got the status of the player
        //useful to know if is ready to play
        //if status is unknown then got the error
    }

    func playerVideo(player: PlayerView, statusItemPlayer: PlayerViewItemStatus, error: NSError?) {
        //some status got here first, this is the status of AVPlayerItem
        //useful to know if is ready to play
        //if status is unknown then got the error
    }

    func playerVideo(player: PlayerView, loadedTimeRanges: [PlayerviewTimeRange]) {
        //got the buffer of the video
        //to know the progress loaded

        //this will get the seconds of the end of the buffer
        //loadedTimeRanges.first!.end.seconds

    }

    func playerVideo(player: PlayerView, duration: Double) {
        //the player knows the duration of the video to reproduce on seconds
    }

    func playerVideo(player: PlayerView, currentTime: Double) {
        //executed using the playerVideo.interval
        //only executed when the is reproducing, on pause (rate == 1) this doesn't execute
        //default executed like 60 frames per seconds, so 60 times on a second
    }

    func playerVideo(player: PlayerView, rate: Float) {
        //if the speed of reproduction changed by pausing, playing or changing speed
    }

    func playerVideo(playerFinished player: PlayerView) {
        //when the video finishes the reproduction to the end
    }
}

其他信息

  • 如果您喜欢其他快捷方式,有更好的实现方法,或者只是表示感谢,请发送电子邮件至 [email protected]

  • 我在 http 上使用视频,因此我需要在 info.plist 上添加此内容以接受所有 http 上的内容,这在生产中使用并不安全,因此最好是只添加受信任的域名或使用 https

<key>NSAppTransportSecurity</key>
<dict>
    <!--Include to allow all connections (DANGER)-->
    <key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

作者

David Alejandro, [email protected]

许可证

PlayerView 在 MIT 许可下提供。有关更多信息,请参阅 LICENSE 文件。