took a bit of experimenting...
21. playerView.translatesAutoresizingMaskIntoConstraints = false
22.
23. view.addSubview(playerView)
24.
25.
26.
27. playerView.leadingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.leadingAnchor ).isActive = true
28.
29. playerView.trailingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
30.
31. playerView.topAnchor.constraint (equalTo: view.safeAreaLayoutGuide.topAnchor ).isActive = true
32.
33. playerView.bottomAnchor.constraint (equalTo: view.safeAreaLayoutGuide.bottomAnchor ).isActive = true
34.
35.
36.
37. playerView.controlsStyle = .floating
38.
39. playerView.showsFrameSteppingButtons = true
40.
41. playerView.showsFullScreenToggleButton = true
42.
43.
44.
45. guard let path = Bundle.main.url(forResource: "myMovie", withExtension: "mov") else { return }
46.
47.
48.
49. let player = AVPlayer(url: path)
50.
51. playerView.player = player
52.
53. playerView.player?.play()
54.
55. }
56.
57. }
The StoryBoard:AttributesInspector:AVPlayerView settings don't work for me... exactly. I had to set StoryBoard:AttributesInspector:AVPlayerView:ControlsStyle:none then include lines 37-41 in my code.
The default is StoryBoard:AttributesInspector:AVPlayerView:ControlsStyle:inline which appears on screen but does nothing, i.e., no control.
Line 21 is also required to be false.
The .mov file can be File:AddFilesTo 'd or copied to Assets.xcassets
see also:
Developer Forum:
"can't get extremely simple macOS video player to work..."
https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_macos
import AVKit // macOS & iOS
class ViewController_myShow: NSViewController {
@IBOutlet var myMOV: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
let playerView = AVPlayerView()
21. playerView.translatesAutoresizingMaskIntoConstraints = false
22.
23. view.addSubview(playerView)
24.
25.
26.
27. playerView.leadingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.leadingAnchor ).isActive = true
28.
29. playerView.trailingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
30.
31. playerView.topAnchor.constraint (equalTo: view.safeAreaLayoutGuide.topAnchor ).isActive = true
32.
33. playerView.bottomAnchor.constraint (equalTo: view.safeAreaLayoutGuide.bottomAnchor ).isActive = true
34.
35.
36.
37. playerView.controlsStyle = .floating
38.
39. playerView.showsFrameSteppingButtons = true
40.
41. playerView.showsFullScreenToggleButton = true
42.
43.
44.
45. guard let path = Bundle.main.url(forResource: "myMovie", withExtension: "mov") else { return }
46.
47.
48.
49. let player = AVPlayer(url: path)
50.
51. playerView.player = player
52.
53. playerView.player?.play()
54.
55. }
56.
57. }
The StoryBoard:AttributesInspector:AVPlayerView settings don't work for me... exactly. I had to set StoryBoard:AttributesInspector:AVPlayerView:ControlsStyle:none then include lines 37-41 in my code.
The default is StoryBoard:AttributesInspector:AVPlayerView:ControlsStyle:inline which appears on screen but does nothing, i.e., no control.
Line 21 is also required to be false.
The .mov file can be File:AddFilesTo 'd or copied to Assets.xcassets
see also:
Developer Forum:
"can't get extremely simple macOS video player to work..."
https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/creating_a_basic_video_player_macos
Code Block
Belated follow-up to my own question. The following code works, and is not much different than that already posted. So my problem must have been in the storyboard.
import AVKit // macOS & iOS
class ViewController_myProduction: NSViewController {
@IBOutlet var myMOV: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
let playerView = AVPlayerView()
playerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(playerView)
if #available(macOS 11.0, *) {
playerView.leadingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.leadingAnchor ).isActive = true
playerView.trailingAnchor.constraint (equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
playerView.topAnchor.constraint (equalTo: view.safeAreaLayoutGuide.topAnchor ).isActive = true
playerView.bottomAnchor.constraint (equalTo: view.safeAreaLayoutGuide.bottomAnchor ).isActive = true
} else {
// Fallback on earlier versions
}
playerView.controlsStyle = .floating
playerView.showsFrameSteppingButtons = true
playerView.showsFullScreenToggleButton = true
guard let path = Bundle.main.url(forResource: "myProduction", withExtension: "mov") else { return }
let player = AVPlayer(url: path)
playerView.player = player
playerView.player?.play()
}
}
note to self: code-block + Edit:"Paste and Match Style"