3

I have a URL :- "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1" When I run this URL on browser, My VDO starts downloads.

Please help to play this video in a view (let view name is:- vdoView)

For this I am trying below code:-

import UIKit
import AVKit
import AVFoundation

class VideoViewController: UIViewController {

    @IBOutlet weak var vdoView: UIView!

    override func viewDidLoad() {

        super.viewDidLoad()

        getVideo()
    }

    func getVideo(){

        let videoURL = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")

        let player = AVPlayer(url: videoURL!)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.vdoView.bounds
        self.vdoView.layer.addSublayer(playerLayer)
        player.play()

    }

enter image description here

Nic3500
  • 8,144
  • 10
  • 29
  • 40
piyush ranjan
  • 391
  • 4
  • 15

4 Answers4

4

First you have to import AVKit, import AVFoundation

Using AVPlayer

  if let url = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1"){

            let player = AVPlayer(url: url)
            let controller=AVPlayerViewController()
            controller.player=player
            controller.view.frame = self.view.frame
            self.view.addSubview(controller.view)
            self.addChildViewController(controller)
            player.play()
        }

It's better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Amit gupta
  • 553
  • 3
  • 10
3
override func viewDidLoad()

    {
       let videoURL = NSURL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")
        let player = AVPlayer(url: videoURL! as URL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }


    }
Soham Ray
  • 215
  • 2
  • 4
2

You should try it like this

func getVideo(){

    let videoURL = URL(string: "http://fitnation.theclientdemos.com:9000/media/uploads/videoplayback_3_JtVCHi1")
    // Create an AVAsset
    let videoAsset = AVAsset(url: videoURL!)
    // Create an AVPlayerItem with asset
    let videoPlayerItem = AVPlayerItem(asset: videoAsset)
    // Initialize player with the AVPlayerItem instance.
    let player = AVPlayer(playerItem: videoPlayerItem)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.vdoView.bounds
    self.vdoView.layer.addSublayer(playerLayer)
    player.play()
}

And you should update your plist file to allow the contents from http: Refer here

Bhavin Kansagara
  • 2,866
  • 1
  • 16
  • 20
  • thanku for reply, I tried your code but still got the same error :-Task <7C4E2AE9-F629-44C8-9CB8-45719FA5D76A>.<1> finished with error - code: -999 – piyush ranjan Sep 18 '18 at 12:58
  • 1
    Refer here for more about the error code and fix it: https://stackoverflow.com/questions/16073519/nsurlerrordomain-error-code-999-in-ios – Bhavin Kansagara Sep 18 '18 at 13:37
  • And can you confirm, that you have set the transport security as per follows: https://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi/32631185#32631185 – Bhavin Kansagara Sep 18 '18 at 13:40
1

*All you need is change your code to this:

import AVFoundation

class your class name

var player: AVPlayer?

func playAudio() {
    guard let url = URL.init(string: "http://192.168.100.184:9050/uploads/sound/file/1/a1b19343-f785-4d48-b7d5-1abe26c03ff3.mp3" ) else { return }

    player = AVPlayer.init(url: url)
}

and at the end call the func in IBAction body*

just like the code below:

@IBAction play (sender: Any) {
playAudio()
}
Negar
  • 64
  • 6