4

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

enter image description here

Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30
zavrina
  • 305
  • 3
  • 14
  • Can you check this-> instead of adding playercontroller as childcontroller.. present/push it. Is is working now?. If yes, then atleast we comes to know that.. this is due to adding childviewcontroller otherwise, there is some other problem with AVPlayer. – Mehul Thakkar Jun 28 '16 at 04:53
  • @Mehul, I think so. I will try it out. – zavrina Jun 28 '16 at 04:55
  • Also, check by debugging that fileurlpath, that you used is proper or not. sometimes, path using fileurlpath makes problem, so try other way too – Mehul Thakkar Jun 28 '16 at 04:55
  • @MehulThakkar, I already check file url path and I can see file path receiving from server. – zavrina Jun 28 '16 at 04:57
  • 1
    If you are using server url.. then never use fileURLWithPath, Got the problem.. use urlWithString for server url. fileURLWithPath is for local urls. Now it will work. Try. I am 100% sure that, this url have made the problem, am i right? – Mehul Thakkar Jun 28 '16 at 04:58
  • @Mehul, I got this from server. "> Video Link : https://www.youtube.com/watch?v=ao2fdQUgvq4" . It returns string value. I think it's because of ChildViewController. – zavrina Jun 28 '16 at 05:03
  • Have you try with AVPlayerLayer? – Bhadresh Mulsaniya Jun 28 '16 at 05:34
  • @BhadreshMulsaniya, yes. When I tried with AVPlayerLayer, it didn't show even video player. – zavrina Jun 28 '16 at 05:37
  • @ZinMarHtet Have you read my answer? – Bhadresh Mulsaniya Jun 28 '16 at 06:52
  • @BhadreshMulsaniya, Hello, yes, I saw it just now. I will try it out later coz I'm working on other task now. Thank you so much for ur help. – zavrina Jun 28 '16 at 07:07

4 Answers4

2

Finally, I used Swift Youtube Player to embed video. It's super easy to use.

zavrina
  • 305
  • 3
  • 14
1

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

Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
0

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()
}
Community
  • 1
  • 1
Daniel Ormeño
  • 2,743
  • 2
  • 25
  • 30
  • I'm using iOS 8.1. In info.plist, I set "Allow Arbitrary Loads = YES". And I tried by using NSURL(string: videoUrl) too, but result is same. Video is still not playing. – zavrina Jun 28 '16 at 05:01
  • I edited my answer, try that approach from your view controller. Hope it helps! – Daniel Ormeño Jun 28 '16 at 05:06
  • Thanks for your help, still not playing video. – zavrina Jun 28 '16 at 05:13
  • Oops! Sorry I just realized that i missed a line _playerController.player = AVPlayer(URL: streamingURL)_, I updated the code block – Daniel Ormeño Jun 28 '16 at 05:25
  • In my code, I embedded playerController inside of videoContainerView. So beside of your code, I added two lines of code for adding subview into videoContainerView. But result is no different. – zavrina Jun 28 '16 at 05:34
-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];