Home > Software design >  AVPlayer won't play local files until i add them to the project
AVPlayer won't play local files until i add them to the project

Time:08-02

i would like to play video files that are for example on my desktop, but i only get them playing, if i'm adding them to my project.

This works:

guard let path = Bundle.main.path(forResource: "video", ofType:"mov") else {
    return
}

avPlayerView.player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerView.player?.play()

This not:

let path = "/Users/%Username%/Desktop/mov"
avPlayerView.player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerView.player?.play()

I had a project we're a got I running, sadly i lost it.

Does anyone know what I am missing?

Thanks

CodePudding user response:

If it is running on the Simulator, then it can access the Mac file system.

If it is running on a device, then your app will not be able to access files on your Mac.

If you are running it on the simulator; then are you certain that the following is a valid path to the media file?

"/Users/%Username%/Desktop/mov"

Because it does not appear to be a valid path.

(1): %Username% should be your home folder's name.

(2): mov does not appear to be a full file name.

CodePudding user response:

You have the Mac OS tag on your question, so I gather you are trying to do this on your Mac?

Is the app set up to run in the sandbox? If so, it won't have access to anything other than it's app directories (documents, caches, and a few others) and it's bundle.

The other thing you can do is display a file open dialog and then navigate to the file. The act of selecting the file implicitly grants a sandboxed app permission to open it, if I remember correctly (It's been several years since I've done sandboxed file system code for Mac OS, so my memory is a bit vague.)

  • Related