How to play video in ios swift -


i new ios programming, need stream video url unable stream video url using avplayer, using avplayer downloaded file able play.actual problem file format different example file name song.apa song.mp4

code:

let avplayercontroller = avplayerviewcontroller() var avplayer:avplayer?  override func viewdidload() {     super.viewdidload()     let movieurl = url.init(string: "http://techslides.com/demos/sample-videos/small.3gp")     self.avplayer = avplayer.init(url: movieurl!)     self.avplayercontroller.player = self.avplayer }  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }  @ibaction func playvideo(_ sender: any) {     self.present(self.avplayercontroller, animated: true) {       self.avplayercontroller.player?.play()     } } 

in swift 3, try code playing video in project

import uikit import avkit import avfoundation import mediaplayer import mobilecoreservices  class videoplayerviewcontroller: uiviewcontroller,avplayerviewcontrollerdelegate {      //mark: - outlet -     @iboutlet weak var viewvidioplayer: uiview!     //mark: - variable      //mark: - view life cycle -     override func viewdidload() {         super.viewdidload()             }     //mark: - action -     //for playing video     @ibaction func btnvideoplayclicked(_ sender: uibutton) {          self.videoplay()     }      func videoplay()     {         let playercontroller = avplayerviewcontroller()         playercontroller.delegate = self          let bundle = bundle.main         let moviepath: string? = "http://techslides.com/demos/sample-videos/small.3gp"         let movieurl = url(fileurlwithpath: moviepath!)          let player = avplayer(url: movieurl)         playercontroller.player = player         self.addchildviewcontroller(playercontroller)         self.view.addsubview(playercontroller.view)         playercontroller.view.frame = self.view.frame          player.play()      }     //mark: - other function -      func playerviewcontrollerwillstartpictureinpicture(_ playerviewcontroller: avplayerviewcontroller){         print("playerviewcontrollerwillstartpictureinpicture")     }      func playerviewcontrollerdidstartpictureinpicture(_ playerviewcontroller: avplayerviewcontroller)     {         print("playerviewcontrollerdidstartpictureinpicture")      }     func playerviewcontroller(_ playerviewcontroller: avplayerviewcontroller, failedtostartpictureinpicturewitherror error: error)     {         print("failedtostartpictureinpicturewitherror")     }     func playerviewcontrollerwillstoppictureinpicture(_ playerviewcontroller: avplayerviewcontroller)     {         print("playerviewcontrollerwillstoppictureinpicture")     }     func playerviewcontrollerdidstoppictureinpicture(_ playerviewcontroller: avplayerviewcontroller)     {         print("playerviewcontrollerdidstoppictureinpicture")     }     func playerviewcontrollershouldautomaticallydismissatpictureinpicturestart(_ playerviewcontroller: avplayerviewcontroller) -> bool     {         print("playerviewcontrollershouldautomaticallydismissatpictureinpicturestart")         return true     } } 

i hope it's work you, thank


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -