0

Anyone successfully managed to play Google drive video with AVFoundation, AVPlayer?

Objective-c or swift, ios or osx would work fine for me.

What I have tried?

I tried:

  • iOS Stream video from Google drive Swift . Here, If it try to extract link from the url, I got access denied(video is public).

  • Extract link from google drive and use that one, eg. video link, but when I use it in the app, nothing gets loaded.

  • Tried something like this after looking into other solutions for web and stuff, no luck

Link to video

Thanks in advance!

Miknash
  • 7,888
  • 3
  • 34
  • 46
  • Are you able to download the video from any of those urls (from your app I mean) – Giuseppe Lanza Jan 10 '18 at 15:36
  • The main idea is to stream it - but I haven't download it. – Miknash Jan 10 '18 at 15:48
  • Check if you can download it. if you can I'll tell you how to stream it. ;) – Giuseppe Lanza Jan 10 '18 at 15:51
  • Can you provide me some code for it? Thanks for help – Miknash Jan 10 '18 at 15:51
  • You must be sure the url points to a video first. if you can download a video file executable in your app you can stream this file instead of downloading it all at once by using AVAssetResourceLoaderDelegate. Get the URL, make sure it downloads a valid video file executable from your app then I will test and post the code to stream it. There is no point for me to write this code and post an answer if it doesn't work. – Giuseppe Lanza Jan 10 '18 at 15:57

1 Answers1

1

Gdrive does not support the streaming of videos, but it supports the resume on download.

You will need to use a custom AVAssetResourceLoaderDelegate with a valid video URL.

class GDriveLoader: NSObject, AVAssetResourceLoaderDelegate {
    public func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
        //Here you will put your code to download the bytes requested by AVPlayer. 
        //You can find this info in the loadingRequest.dataRequest.
    }
}

In this sample project there are 3 implementations of custom AVAssetResourceLoaderDelegate in objective c.

Basically what you will have to do once you have the download url si to replace its schema to a custom schema. something like gdrive:// instead of https://.

when you create your AVURLAsset with this url you can access to the newly created object's property resourceLoader and call the method setDelegate passing your GDriveLoader.

With a custom schema AVFoundation doesn't know how to fetch the video data, and will ask to the asset's resource loader what to do. The method resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) will be queried multiple times for each chunk of data currently in read by AVPlayer.

Giuseppe Lanza
  • 3,519
  • 1
  • 18
  • 40
  • but then again, this would download the video to hdd without removing? – Miknash Jan 10 '18 at 18:44
  • No. it will download just the needed bytes for avplayer in memory. with a DataTask you must decide what to do with the fetched data. if you don't save them on disk they will not be on disk. Read the comments in the method. you must compose your request to get the bytes requested by the resource loader. in loadingRequest.dataRequest you will find offset and length of the bytes you need. – Giuseppe Lanza Jan 11 '18 at 10:56