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

上下刷视频刷的快的话,必crash #11

Open
yuanzhiying opened this issue Jan 8, 2019 · 1 comment
Open

上下刷视频刷的快的话,必crash #11

yuanzhiying opened this issue Jan 8, 2019 · 1 comment

Comments

@yuanzhiying
Copy link

func respondWithDataForRequest(loadingRequest:AVAssetResourceLoadingRequest) -> Bool {
let mimeType = self.response?.mimeType ?? ""
let contentType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)
loadingRequest.contentInformationRequest?.isByteRangeAccessSupported = true
loadingRequest.contentInformationRequest?.contentType = contentType?.takeRetainedValue() as String?
// 崩溃位置 ---
loadingRequest.contentInformationRequest?.contentLength = (self.response?.expectedContentLength)!

    var startOffset:Int64 = loadingRequest.dataRequest?.requestedOffset ?? 0
    if loadingRequest.dataRequest?.currentOffset != 0 {
        startOffset = loadingRequest.dataRequest?.currentOffset ?? 0
    }
    
    if Int64(data?.count ?? 0)  < startOffset {
        return false
    }
    
    let unreadBytes:Int64 = Int64(data?.count ?? 0) - (startOffset)
    let numberOfBytesToRespondWidth:Int64 = min(Int64(loadingRequest.dataRequest?.requestedLength ?? 0), unreadBytes)
    if let subdata = (data?.subdata(in: Int(startOffset)..<Int(startOffset + numberOfBytesToRespondWidth)))  {
        loadingRequest.dataRequest?.respond(with: subdata)
        let endOffset:Int64 = startOffset + Int64(loadingRequest.dataRequest?.requestedLength ?? 0)
        return Int64(data?.count ?? 0) >= endOffset
    }
    return false
}
@TJDaDa
Copy link

TJDaDa commented Aug 7, 2020

  1. 不要在set里面创建对象, 滑动cell会导致偶尔没释放内存增加堆栈闪退.
  • self?.urlAsset = AVURLAsset.init(url: url, options: nil)
    self?.urlAsset?.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
    if let asset = self?.urlAsset {
    self?.playerItem = AVPlayerItem.init(asset: asset)
    self?.playerItem?.addObserver(self!, forKeyPath: "status", options: [.initial, .new], context: nil)
    self?.player = AVPlayer.init(playerItem: self?.playerItem)
    self?.playerLayer.player = self?.player
  1. 要在初始化里面创建对象, set里面只赋值, 滑动cell就不用每次释放对象了.
  • func initSubView() {
    playerLayer = AVPlayerLayer.init(player: player)
    playerLayer!.videoGravity = .resizeAspect
    self.layer.addSublayer(self.playerLayer!)

     player = AVPlayer()
     playerLayer!.player = player
    

    }

        if let url = sourceURL {
             let urlAsset = AVAsset(url: url)
             let playerItem = AVPlayerItem(asset: urlAsset)
             player!.replaceCurrentItem(with: playerItem)
             player?.currentItem!.addObserver(self, forKeyPath: "status", options: [.initial, .new], context: nil)
             addProgressObserver()
         }
    

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

No branches or pull requests

2 participants