Post

Replies

Boosts

Views

Activity

SwiftUI: VideoPlayer to display different videos stored locally (back and forward buttons)
I'm still new to SwiftUI and have run into a problem. I need to use back and forward buttons to make the Video Player go to the previous/next video (stored locally). The following code works for one video only, the one declared into the init(), but I can't manage to change the video by clicking the back and forward buttons. I'm using an array of String called videoNames to pass all the video names from the previous View. Also, I'm using a custom Video Player for this and I'm gonna include the relevant parts of the code. This is my View: let videoNames: [String] @State var customPlayer : AVPlayer @State var isplaying = false @State var showcontrols = false init(videoNames: [String]) { self.videoNames = videoNames self._customPlayer = State(initialValue: AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: videoNames[0], ofType: "mov")!))) } var body: some View { VStack { CustomVideoPlayer(player: $customPlayer) .frame(width: 390, height: 219) .onTapGesture { self.showcontrols = true } GeometryReader {_ in HStack { // BACK BUTTON Button(action: { // code }, label: { Image(systemName: "lessthan") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 60, height: 60) .foregroundColor((Color(red: 243/255, green: 189/255, blue: 126/255))) .padding() }) // FORWARD BUTTON Button(action: { // code }, label: { Image(systemName: "greaterthan") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 60, height: 60) .foregroundColor((Color(red: 243/255, green: 189/255, blue: 126/255))) .padding() }) } } } .offset(y: 35) .edgesIgnoringSafeArea(.all) } This is my custom Video Player: struct CustomVideoPlayer : UIViewControllerRepresentable { @Binding var player: AVPlayer func makeUIViewController(context: UIViewControllerRepresentableContext<CustomVideoPlayer>) -> AVPlayerViewController { let controller = AVPlayerViewController() controller.player = player controller.showsPlaybackControls = false return controller } func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<CustomVideoPlayer>) { } } I've researched for solutions but couldn't find anything relevant. I've tried to modify my CustomVideoPlayer and not pass a @Binding variable... As well, the init() gave me a lot of headaches as it comes back to errors every time I change something... Any solution would help guys. I really appreciate your time.
0
0
1.6k
Jul ’21
Cannot use instance member 'videoName' within property initializer; property initializers run before 'self' is available
Need URGENT help !! I'm quite new to SwiftUI and I'm running into this problem as I'm trying to display a video using videoName from Model. In the player = AVPlayer(...)(line 4), instead of finding the resource by the string "squats", I want to use videoName from Model. If I replace them I get the error "Cannot use instance member 'videoName' within property initializer; property initializers run before 'self' is available". Can anyone please help me? This code is part of my Fitness App which is the core project of my Dissertation, hence "URGENT". Here is my code: struct ExercisingSessionView: View {          let exerciseName: String     let videoName: String          @State var player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "squats", ofType: "mov")!))     @State var isplaying = false     @State var showcontrols = false        var body: some View {                  CustomVideoPlayer(player: $player)             .frame(width: 390, height: 219)             .onTapGesture {                 self.showcontrols = true                              }     }          struct CustomVideoPlayer : UIViewControllerRepresentable {                  @Binding var player: AVPlayer                  func makeUIViewController(context: UIViewControllerRepresentableContext) -> AVPlayerViewController {                          let controller = AVPlayerViewController()             controller.player = player             controller.showsPlaybackControls = false             return controller         }                  func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext) {                      }     } }
2
0
1.8k
Jul ’21