0

In my iOS app I have a bunch of mp4 videos that I download at a certain time on the app using On Demand Resources. Using this tutorial: https://www.raywenderlich.com/520-on-demand-resources-in-ios-tutorial

I download the resources like this at the start of the app, in a previous view controller:

func requestSceneWith(tag: String,
                  onSuccess: @escaping () -> Void,
                  onFailure: @escaping (NSError) -> Void) {

// 2
currentRequest = NSBundleResourceRequest(tags: [tag])

// 3
guard let request = currentRequest else { return }

request.beginAccessingResources { (error: Error?) in


    // 4
    if let error = error {
        onFailure(error as NSError)
        return
    }

    // 5
    onSuccess()



}

The resource seem to download fine, and I know that they have been downloaded, by looking in the disk report in xcode.

However, when the videos are supposed to be played in the app, the app just shows a black screen. Here is my code to play the videos:

  let videoURL = Bundle.main.url(forResource: "cow2", withExtension: "mp4", subdirectory: "Videos/Animals")

    self.player = AVPlayer(url: videoURL!)

    self.myPlayerController.player = self.player

 self.myPlayerController.player?.play()

Now, when the resources are not tagged, and they come with the app and not downloaded later, they work fine. And the console prints the file name, like I did (print(videoURL.absoluteString). But after they are tagged and downloaded later, they dont work, and nothing prints in the console. Just a black screen appears in the app.

I've been stuck on this for ages, and help with really help.

Thanks

RJB
  • 1,704
  • 23
  • 50
  • Where is your call to https://developer.apple.com/documentation/foundation/nsbundleresourcerequest/1614840-beginaccessingresources ? – matt Apr 01 '19 at 18:07
  • Added code to show beginAccessingResources – RJB Apr 01 '19 at 18:09
  • Ok but fileURLWithPath is not how you do this. You must use a Bundle resource method. – matt Apr 01 '19 at 18:11
  • And don’t forget to get onto the main thread. Really you are still not showing enough code. Show the whole chain of calls. – matt Apr 01 '19 at 18:19
  • I updated code, don't use fileURLWithPath. See code edited. But it still don't work. Thanks – RJB Apr 01 '19 at 18:54
  • It is much easier with https://github.com/maxvol/RxOnDemandResources which I am using for more than a year. Just saying. – Maxim Volgin Apr 07 '19 at 19:53

1 Answers1

0

I think you didn't request the on-demand resources before accessing the video file. Maybe you can try to declare the NSBundleResourceRequest instance as a global variable in AppDelegate.

Amos Hsueh
  • 211
  • 4
  • 11