Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

预览网络视频的时候,如何设置用户身份验证信息? #642

Closed
aluzi992 opened this issue Aug 19, 2021 · 2 comments
Closed

Comments

@aluzi992
Copy link

版本:4.1.5
请问,使用ZLImagePreviewController去预览网上的视频(mp4),视频需要用户名和密码才可以下载,我应该如何将用户验证信息(例如cookie或者密码)传给你?我用如下代码:
let videoSuffixs = ["mp4", "mov", "avi", "rmvb", "rm", "flv", "3gp", "wmv", "vob", "dat", "m4v", "f4v", "mkv"] // and more suffixs
let vc = ZLImagePreviewController(datas: datas, index: index, showSelectBtn: false) { (url) -> ZLURLType in
if let sf = url.absoluteString.split(separator: ".").last, videoSuffixs.contains(String(sf)) {
return .video
} else {
return .image
}
}
urlImageLoader: {......}

预览视频的时候,黑屏没显示。查看过HTTP复服务器的日志,确认你请求视频文件的时候,并没有通过用户验证
如果是图片的话,按照你的例子,可以用kf去加载,而kf有设置身份验证的接口,但是视频应该怎么设置呢?

@longitachi
Copy link
Owner

你先在 ZLPhotoPreviewCell 中的 728 行代码 (ZLNetVideoPreviewCell 这个类)做如下修改看是否可以正常播放

func configureCell() {
    ...

    let asset = AVURLAsset(url: self.videoUrl, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
    let item = AVPlayerItem(asset: asset)
    self.player = AVPlayer(playerItem: item)

    ...
}

将你的token放到这个headers里,如果可以的话,我这边支持下。

@aluzi992
Copy link
Author

aluzi992 commented Aug 24, 2021

我用SwiftPackage安装的,代码不能修改,我直接在自己的项目里面做了如下测试:
let headers: [String: String] = [
"Authorization": "Basic xxxxxxxxxxxxxxx"
]
let asset = AVURLAsset(url: urlString, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated:true, completion: nil)

确认可以播放,并且查看过服务器的日志,确认身份验证信息是正确的
不过 有一点要注意,视频文件不能压缩(有些http服务器你上传附件的时候会压缩保存的),如果是压缩存储的,则无法播放,AVPlayer会报错说“不支持ByteRanges”,可能就是断点续传的意思吧...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants