简体   繁体   中英

Can't bind button to AVPlayer

I am trying to make a detailed view in swift, but I just can't figure out a way to pause the video with a custom button. And also when I go back to my list I can still hear the video playing in the background. Here is my code for the AVPlayer and for the button.

 import SwiftUI
 import AVKit

 struct Workdetail: View {



var work: WorkoutDe



   @State var player = AVPlayer()
   @State var isplaying = true




var body: some View {
    
    
    VStack {
    ZStack {
      
        VideoPlayer(player: $player, work: work)
                .frame(height: UIScreen.main.bounds.height / 3.5)
     
        Butto(player: $player, isplaying: $isplaying)
        
    }
    Spacer()
    }
                   }


        
    

 }
         struct Butto : View {
                
              @Binding var player : AVPlayer
              @Binding var isplaying : Bool
               
                
                
                var body : some View{
                
                
                    Button(action: {
                        
                        if self.isplaying{
                            
                            self.player.pause()
                            self.isplaying = false
                        }
                        else{
                            
                            self.player.play()
                            self.isplaying = true
                        }
                        
                    }) {
                        
                        Image(systemName: self.isplaying ? "pause.fill" : "play.fill")
                            .font(.title)
                            .foregroundColor(.white)
                            .padding(20)
                    }
                    
                }       
                
    }



 

    struct VideoPlayer : UIViewControllerRepresentable {
            
        
        var work : WorkoutDe
        
        @Binding var player : AVPlayer
    
                var playerLayer = AVPlayerLayer()
        
         public func makeUIViewController(context: Context) -> AVPlayerViewController {
        
    
               let player = AVPlayer(url: URL(fileURLWithPath: String(work.url)))
            let controller = AVPlayerViewController()
            controller.player = player
            controller.videoGravity = .resizeAspectFill

            player.actionAtItemEnd = .none
            NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) { _ in
                player.seek(to: CMTime.zero)
              player.play()
              
            }
        
            
            
            player.play()
  
            
            return controller
            
        }
    
        
        func rewindVideo(notification: Notification) {
             playerLayer.player?.seek(to: .zero)
         }
    
    public func updateUIViewController(_ uiViewController: AVPlayerViewController, context: UIViewControllerRepresentableContext<VideoPlayer>) {
          
          
      }
}

The AVPlayer works but when I press the button nothing happens. The image for the button changes but the video won't stop playing. Can someone please explain to me how I can bind the button, because I can't figure it out

Here is the code for a the problem, if you guys are looking for it. Someone answered it on another post:

 public func makeUIViewController(context: Context) -> AVPlayerViewController {

let player = AVPlayer(url: URL(fileURLWithPath: String(work.url)))
let controller = AVPlayerViewController()

DispatchQueue.main.async {
   self.player = player
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM