How do you play a video with AV Kit Player View Controller in Swift?
override func viewDidLoad() {
super.viewDidLoad()
let videoURLWithPath = "http://****/5.m3u8"
let videoURL = NSURL(string: videoURLWithPath)
playerViewController = AVPlayerViewController()
dispatch_async(dispatch_get_main_queue()) {
self.playerViewController?.player = AVPlayer.playerWithURL(videoURL) as AVPlayer
}
}
Objective c
This only works in
Xcode 7
Go to
.h
file and importAVKit/AVKit.h
andAVFoundation/AVFoundation.h
. Then go.m
file and add this code:a bug(?!) in iOS10/Swift3/Xcode 8?
does not work (empty rect...)
this works:
Same URL...
Swift 3:
Swift 3.x - 4.x
Necessary: import AVKit, import AVFoundation
AVFoundation framework is needed even if you use AVPlayer
If you want to use AVPlayerViewController:
or just AVPlayer:
It's better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.
Objective-C
AVPlayerViewController:
or just AVPlayer:
Try This
Using MPMoviePlayerController :
Using AVPlayer :
I have a play button to handle button tap.
I have added an observer listening for AVPlayerItemDidPlayToEndTimeNotification.
When video/audio play is finished, reset button image and notification