Reputation: 305
I am trying to embed video player using Swift. When I run the app, I can see the video player but video is not playing. Could you check out any missing point, please? Thanks in advance.
var playerItem: AVPlayerItem?
var player: AVPlayer?
if let videoLink = newLaunch.videoLinks where newLaunch.videoLinks!.count > 0{
let videoUrl = videoLink[0]
let streamingURL: NSURL = NSURL(fileURLWithPath: videoUrl)
player = AVPlayer(URL: streamingURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.videoContainerView.addSubview(playerController.view)
playerController.view.frame = self.videoContainerView.bounds
player!.play()
}
Upvotes: 4
Views: 5885
Reputation: 1
you can add the AVPlayer as a layer on the view:
player = [[AVPlayer alloc] init....
playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
CGSize size = self.view.bounds.size;
float x = size.width/2.0-187.0;
float y = size.height/2.0 - 125.0;
playerLayer.frame = CGRectMake(x, y, 474, 320);
[playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.view.layer addSublayer:playerLayer];
[player play];
Upvotes: -1
Reputation: 305
Finally, I used Swift Youtube Player to embed video. It's super easy to use.
Upvotes: 2
Reputation: 2640
I think there is no way apart from UIWebView
to play youtube video inside iOS application.
Also you can redirect to youtube application by passing your video url.So youtube application will handle rest of the things.
You can read the guidelines of YouTube section 2 point 10 :https://developers.google.com/youtube/terms?hl=en
You can use youtube player : https://developers.google.com/youtube/v3/guides/ios_youtube_helper#installation
Upvotes: 1
Reputation: 2778
What version of IOS are you using? it looks like this might be related to Apple's new transport layer security enhancements.
You need to enable your app to allow streaming video from that particular domain. You can alternatively bypass the transport layer security for while you develop your app.
Check out this thread for how to do this: https://stackoverflow.com/a/36305505/5229157
Also, you might want to use a different url constructor for streaming your file:
let videoUrl = videoLink[0]
let streamingURL = NSURL(string: videoUrl) //- assuming videoLink is of type [String]
player = AVPlayer(URL: streamingURL)
Edit:
Try the following code:
let videoUrl = videoLink[0]
let streamingURL = NSURL(string: videoUrl)
let playerController = AVPlayerViewController()
playerController.player = AVPlayer(URL: streamingURL)
self.presentViewController(playerController, animated: true) {
self.playerController.player?.play()
}
Upvotes: 0