如何设置视频播放器的缩略图
How set thumbnails image of video player
我是新人swift
我正在创建视频播放器,它有后台播放功能,但是当我进入后台或锁定时 iPhone 然后播放、暂停、下一步、上一个控件在那里,但视频播放视频的缩略图未设置 ...
我可以设置视频缩略图吗
像这样
有方法可以设置吗
谢谢
对于 Swift 2,要从视频中创建图像,请使用此代码:
var thumbImage: UIImage?
let fileURL = NSURL(fileURLWithPath: filePath)
let asset = AVAsset(URL: fileURL)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
thumbnailImage = UIImage(CGImage: cgImage)
}
现在你有图像了。要覆盖它,您必须使用 MPNowPlayingInfo。更新信息时添加这段代码:
// works to do to initialise now playing info
if let artwork = thumbImage {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: artwork)
} else {
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
}
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo
我是新人swift
我正在创建视频播放器,它有后台播放功能,但是当我进入后台或锁定时 iPhone 然后播放、暂停、下一步、上一个控件在那里,但视频播放视频的缩略图未设置 ...
我可以设置视频缩略图吗
像这样
有方法可以设置吗
谢谢
对于 Swift 2,要从视频中创建图像,请使用此代码:
var thumbImage: UIImage?
let fileURL = NSURL(fileURLWithPath: filePath)
let asset = AVAsset(URL: fileURL)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
if let cgImage = try? assetImgGenerate.copyCGImageAtTime(time, actualTime: nil) {
thumbnailImage = UIImage(CGImage: cgImage)
}
现在你有图像了。要覆盖它,您必须使用 MPNowPlayingInfo。更新信息时添加这段代码:
// works to do to initialise now playing info
if let artwork = thumbImage {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(image: artwork)
} else {
nowPlayingInfo[MPMediaItemPropertyArtwork] = nil
}
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlayingInfo