Search code examples
xcodemacosswiftuiavfoundationavkit

SwifUI MacOS App AVPlayer not working with Video URL


I gave permission in the info.plist file for http request on the ios side. I have allowed the same for the macOS app but cannot view the video.

Error: Code=-1003 "A server with the specified hostname could not be found."

enter image description here

enter image description here

AVPlayer

import AVKit
struct AVPlayerControllerRepresented : NSViewRepresentable {
    var player = AVPlayer()
    @Binding var playerStatus: PlayerStatus
    
    func makeNSView(context: Context) -> AVPlayerView {
        let view = AVPlayerView()
        view.controlsStyle = .none
        view.player = player
        
        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
            self.player.seek(to: CMTime.zero)
            self.player.play()
        }
        return view
    }
    
    func updateNSView(_ nsView: AVPlayerView, context: Context) {
        switch playerStatus {
        
        case .start:
            return player.play()
        case .end:
            return player.pause()
        }
    }
}
enum PlayerStatus {
    case start
    case end
}

AVPlayer Using

AVPlayerControllerRepresented(player: AVPlayer(url: URL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!), playerStatus: $playerStatus)

Solution

  • I solved my problem this way. I have allowed incoming and outgoing connections under the App Sandbox tab. enter image description here