Search code examples
swiftuiavkit

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value on an mp4 file I've added to project


I'm unable to figure out why I'm getting this "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" error when the file is clearly in the project and is an mp4 file. I've imported the proper AVKit, etc. and still cannot figure this out

enter image description here

 @State var player = AVPlayer(url:  Bundle.main.url(forResource: "bump", withExtension: "mp4")!)
    
    var body: some View {
        VStack {
            VideoPlayer(player: player)
                .allowsHitTesting(false)
        }
        .padding()
    }

I've even replaced the player with the following code and have gotten the same result

AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "bump", ofType: "mp4")!))

Solution

  • Double-check to make sure bump.mp4 was added into bundle resources.

    Project -> Target -> Build Phases -> Copy Bundle Resources -> Tap (+)

    enter image description here