2017-11-21 5 views
0

현재 Swift의 URL에서 iOS에 대한 비디오를 재생하려고합니다. ".mp4"또는 ".m4v"와 같은 결말을 가진 URL은 작동하지만 Dropbox에서 공유 된 동영상 링크를 재생하려고하면 URL이 재생되지 않습니다. 나는 이미 Dropbox 링크의 끝을 "dl = 1"로 변경했지만 어쨌든 아무 일도 일어나지 않습니다.URL에서 Swift로 비디오 재생

네이티브 iOS 플레이어를 원하기 때문에 webview에서 Youtube-video를 재생하는 것은 해결책이 아닙니다.

누구나 Dropbox에서 URL을 재생하려면 어떻게해야하는지 알고 있습니까? 그렇지 않은 경우 클라우드 서비스를 권장 할 수 있습니까?

감사합니다.

+0

처럼 시도하고 당신은 [SwiftyDropbox] (https://github.com/dropbox/SwiftyDropbox가) – zombie

+0

아니, 난 그냥 비디오를 가지고 좋은 솔루션을 필요로 사용하지 않으 AVPlayer에서 재생할 URL –

답변

0

드롭 박스 스트리밍 사양을 알지 못했습니다. 그러나 dropbox url에 mp4와 같은 재생에 대한 힌트가없는 경우 프록시 서버와 같은 AVAssetLoaderDelegate를 사용할 수 있습니다.

AVAssetLoaderDelegate의 excute에. "foo"스키마를 사용하면 'shouldWaitForLoadingOfRequestedResource'가 호출됩니다. 그리고 동일한 http 헤더를 가진 dropbox에 대한 또 다른 요청을해야합니다.

func play() { 
    let url = URL(string: "foo://bar.mp4")! 
    let asset = AVURLAsset(url: url) 
    let playerItem = AVPlayerItem(asset: asset) 
    asset.resourceLoader.setDelegate(self, queue: nil) 
    player.replaceCurrentItem(with: playerItem) 

    player.play() 
} 

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool { 
    var newRequest = URLRequest(url: URL(string: "dropbox url")!) 
    newRequest.allHTTPHeaderFields = loadingRequest.request.allHTTPHeaderFields 

    let sessionTask = URLSession.shared.dataTask(with: newRequest) { data, response, error in 
     if let responseData = data { 
      loadingRequest.dataRequest?.respond(with: responseData) 
     } 
     loadingRequest.finishLoading() // Let player know that finish loding 
    } 

    sessionTask.resume() 

    return true // Wait for sessionTask response. 
} 
+0

"play"-function에서 playerItem은 결코 사용되지 않고 resourceLoader의 sessionTask도 사용되지 않습니다. 이게 어떻게 구현되어야할지 모르겠다면, 조금 더 설명 할 수 있다면 좋을 것이다. –

+0

나는 내 대답을 더 자세히 설명해 주었다. :) – woosiki

+0

업데이트 해 주셔서 감사합니다. 먼저 "asset.resourceLoader.setDelegate (self, queue : nil)"을 "asset.resourceLoader.setDelegate (self, queue : queue : DispatchQueue.main)"로 변경해야합니다. 그렇지 않으면 충돌이 발생합니다. 그 후에 무언가를로드했지만 비디오를 재생할 수 없었습니다. 아마도 Dropbox 링크가 있기 때문일 수도 있습니다. –