无法使用自定义按钮暂停 AVPlayer

Can't pause AVPlayer with custom button

我正在尝试在 swift 中进行详细查看,但我就是想不出使用自定义按钮暂停视频的方法。而且,当我返回我的列表时,我仍然可以听到后台播放的视频。这是我的 AVPlayer 和按钮代码。

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 {
        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>) {
          
    }
}

AVPlayer 可以工作,但是当我按下按钮时没有任何反应。按钮的图像发生变化,但视频不会停止播放。有人可以向我解释一下如何绑定按钮吗,因为我想不通

您在子视图中与不同的玩家一起工作,尝试

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

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

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

绑定应该更新父状态,这将更新 Butto,所以它应该工作。