How do I add local videos to my app in Swift UI?

I've tried numerous methods on the internet but none have worked. Can anybody help? It seems impossible
Answered by OOPer in 665776022

How do I mute the video in Swift UI, as this would sort my problem entirely?

It's hard to say without seeing the view transition of your app.
So just a simplified example:
Code Block
struct ContentView: View {
let avPlayer = AVPlayer(url: Bundle.main.url(forResource: "video", withExtension: "mov")!)
var body: some View {
VideoPlayer(player: avPlayer)
.onDisappear {
avPlayer.isMuted = false
}
}
}

Where you can access avPlayer, you can call methods (or modify properties) of AVPlayer in some action closure appropriate.

If the view transitions of your app is more complex and you need to call AVPlayer methods from within more Views, you may need to consider how to share the same instance avPlayer among such Views.
What do you mean by local videos? Are they video files? If so, I do not understand what you think is difficult, it is as easy as adding images to your project.


By the way, you are not finishing any of your previous posts.
If any of the replies solved your issue, please mark the answer as SOLVED.
If you solved your issue by yourself (including watching other sites), please post the answer by yourself and mark it SOLVED.
Hi, yes video files. I can't get them to play. I've tried using AVKit but idk if that's right

Hi, yes video files. I can't get them to play. I've tried using AVKit but idk if that's right

So, your issue is playing the video added to the project, not adding the video to the project.

First of all, please check the video files are added to the target:
  • Select a video file in the Project navigator

  • Find Target Membership in the File Inspector, and check there is a checked check mark on your app target.


Assuming you have checked above and all OK, this is a simple video playing view:
Code Block
import SwiftUI
import AVKit
struct ContentView: View {
let avPlayer = AVPlayer(url: Bundle.main.url(forResource: "video", withExtension: "mov")!)
var body: some View {
VideoPlayer(player: avPlayer)
}
}

(Assuming video.mov is correctly added to the app target. Please do no forget that file system of iOS is case-sensitive.)

.
I've now got the video file to work but pausing the video the sound of it still is playing while using the app on other pages
How do I mute the video in Swift UI, as this would sort my problem entirely?
Accepted Answer

How do I mute the video in Swift UI, as this would sort my problem entirely?

It's hard to say without seeing the view transition of your app.
So just a simplified example:
Code Block
struct ContentView: View {
let avPlayer = AVPlayer(url: Bundle.main.url(forResource: "video", withExtension: "mov")!)
var body: some View {
VideoPlayer(player: avPlayer)
.onDisappear {
avPlayer.isMuted = false
}
}
}

Where you can access avPlayer, you can call methods (or modify properties) of AVPlayer in some action closure appropriate.

If the view transitions of your app is more complex and you need to call AVPlayer methods from within more Views, you may need to consider how to share the same instance avPlayer among such Views.
I'm having the same issue, which is my video file isn't playing. It's added to the correct target membership with AVKit framework imported. The Bundle.main.url is correct and matches file name. Any help would be much appreciated!

Any help would be much appreciated!

You should better start your own thread.
thanks ;)
How do I add local videos to my app in Swift UI?
 
 
Q