[英]AVPlayer sometimes doesn't load a video
我有一個按鈕和AVPlayer。 單擊按鈕后,我使用DBAttachmentPickerController錄制視頻,我想將其加載到AVPlayer。
在下面的函數中我嘗試加載它:
func refresh(attachmentInfo: AttachmentInfo) {
self.videoLayer.player = nil
if let url = attachmentInfo.url {
self.player = AVPlayer(url: url) // line A
self.videoLayer.player = self.player // line B
}
}
在6/10的情況下它工作正常,但有時視頻不會加載到AVPlayer。
當我在A行和B行設置斷點時,它始終有效。
有任何想法嗎?
我也像你一樣打這個問題。 在我的情況下, AVPlayer
和AVPlayerLayer
位於tableview單元格內,這使問題變得更糟。 但是,我使用的是返回實際視頻網址的API,以及一個由視頻的第一幀圖像組成的圖片網址。 在tableview單元格邏輯中,我首先嘗試將視頻加載到AVPlayer
,然后檢查AVPlayer
是否有任何.video
資產,如果沒有,那么我只需將Image從其他URL加載到UIView
(使用UIImageView
)當然)在圖像的中心有一個“播放”按鈕圖像。 當用戶點擊播放按鈕時,選擇器功能應嘗試使用視頻URL再次加載AVPlayer
。 如果視頻加載且AVPlayer
具有資產,則play()
否則只return
(並可能提醒用戶視頻無法加載)。
如果您沒有視頻第一幀的圖像,則只需使用占位符圖像和按鈕即可。
檢查player
是否有視頻的方法 -
if player.currentItem?.asset.tracks(withMediaType: .video).count == 0{
print("Oops! no video here")
//load an image, and add a button here
}else{
//do whatever needs to be done if video is available
}
不幸的是,除非你有一些可靠的網絡代碼,你可以在utility
線程中下載視頻並將其存儲在緩存中,但除此之外沒有其他可以做的事情。但是你必須再次認識到用戶的數據使用情況,產生過多的下載線程等
對於UITableView
單元中的圖像緩存,我使用了來自Github的SDWebImage
庫 ,這樣我就可以(有點)確保我的圖像(對於視頻)至少會在網絡中斷或網絡中斷的情況下進行緩存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.