0

I would like to play a video file in my ViewController which is loaded in every page of my PageViewController. As you will be able to see I use a plugin called Carlos to cache the videos (which initially need to be downloaded from a server) so that they do not have to be downloaded every time the user hits a new page. However, I can't figure a way out how to play this downloaded file (NSData). Thus, I would really like to know how I can get the URL of the downloaded file so that I can play it using AVPlayer.

Code (still using URL from server)

let omniCache = videoCache.cache

let request = omniCache.get(URL(string: video!)!)

request
    .onSuccess { videoFile in

        print("The file..." )
        print(videoFile)

        //How can I get the local URL here instead of my server url
        if let videoURL = URL(string: self.video!){

            if self.player == nil {

                let playerItemToBePlayed = AVPlayerItem(url: videoURL as URL)

                self.player = AVPlayer(playerItem: playerItemToBePlayed)

                let playerLayer = AVPlayerLayer(player: self.player)
                playerLayer.frame = self.view.frame
                self.controlsContainerView.layer.insertSublayer(playerLayer, at: 0)

            }

        }

    }
    .onFailure { error in

        print("An error occurred :( \(error)")

    }
Moritz
  • 745
  • 1
  • 10
  • 32
  • If the file is cached on disk, why not just generate a URL to the local file path and use that/ – JAL Jun 14 '17 at 15:53
  • @JAL tried it but couldn't figure out how to get the URL and I didn't find a function provided by the library which would do so. :/ Do you know a way to find the local url? – Moritz Jun 14 '17 at 15:55
  • [How can I retrieve local files with NSURL?](https://stackoverflow.com/q/28419188/2415822) – JAL Jun 14 '17 at 15:56
  • But where would I enter my `videoFile` in the code shown in the answer? – Moritz Jun 14 '17 at 16:02

1 Answers1

0

Look at this code of yours:

    videoFile in

    print("The file..." )
    print(videoFile)

    if let videoURL = URL(string: self.video!){

So in the first line you print videoFile, which turns out to be the data of the file. But then you ignore it! You never mention videoFile again. Why do you ignore it? That is the data, you already have the data. Now play it!

If the data is a file, get its file URL and play it. If it is in memory — it definitely should not be, because a video held entirely in memory would crash your program — save it, and get that file URL and play it.

[I have to ask, however, why you are interposing this cache plug-in between yourself and such a simple task. Why don't you just download the remote video to disk, yourself?]

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I know I am not using the data I can't find the local url and don't know how I can play it so that's why I showed what I had until now... I still do not know how the last comment on my question ("How to retrieve local files with NSURL") helps me since I don;t know where I can enter the `videoFile`... – Moritz Jun 14 '17 at 16:02
  • The library comes with some other functions that make caching a lot simpler in various other cases. In addition to that, it has a memory cache. – Moritz Jun 14 '17 at 16:05