Question: Why is this code not working? It is a simple app for playing a video to users with a password snd username. I want to
Why is this code not working? It is a simple app for playing a video to users with a password snd username.
I want to use a video that i have on my mac book to add it onto the sheet. Please stop answering with openai thats super annoying. The file is a valid mp3 format and it is the correct path to the file. The video gets displayed black.
import SwiftUI
import AVKit
struct ContentView: View {
@State private var username = ""
@State private var password = ""
@State private var showError = false
@State private var showSuccess = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.green)
TextField("Username", text: $username)
.textFieldStyle(RoundedBorderTextFieldStyle())
SecureField("Password", text: $password)
.textFieldStyle(RoundedBorderTextFieldStyle())
if showError {
Text("not possible to log in, wrong username or password")
.font(.callout)
.foregroundColor(.red)
}
if showSuccess {
Text("Successful login.")
.font(.callout)
.foregroundColor(.green)
NavigationLink(destination: VideoView()) {
Text("Go to video")
}
}
Button(action: {
if self.username == "user" && self.password == "pw" {
self.showSuccess = true
} else {
self.showError = true
}
}) {
Text("Log In")
.font(.callout)
.foregroundColor(.green)
}.sheet(isPresented: $showSuccess, content: {
VideoView()
})
}
.padding()
}
}
struct VideoView: View {
let path = "(myfilepath)"
var body: some View {
NavigationView {
VStack {
VideoPlayer(player: AVPlayer(url: URL(fileURLWithPath: self.path)))
}
}
}
}
The path was correct and the file was an mp3 file so a valid format.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
