3

I am new to ios programming, actually I need to stream video URL but i am unable to stream video URL using avplayer, but using avplayer downloaded file i am able to play.Actual problem is my file format is different for example my file name is like this song.apa but it is 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 any resources that can be recreated.
}

@IBAction func playVideo(_ sender: Any)
{
    self.present(self.avplayerController, animated: true) { 
     self.avplayerController.player?.play()
    }
}
skyshine
  • 2,767
  • 7
  • 44
  • 84

7 Answers7

4

In Swift 3

import Foundation
import AVFoundation
import MediaPlayer
import AVKit


func play()
    {
        let player = AVPlayer(url: "Your Url")
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true)
        {
            playerViewController.player!.play()
        }


    }
Amul4608
  • 1,390
  • 14
  • 30
2

You cant play this particular link (http://techslides.com/demos/sample-videos/small.3gp) using AVPlayer or AVPlayerViewController because that file is in AMR (Adaptive Multi-Rate, a format for speech) - which is not supported since iOS 4.3 Some of 3gp files can be played but only ones that have video and audio streams supported by Apple.

Ivan Skrobot
  • 311
  • 3
  • 7
1

In Swift 3, try this code for 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 for you, thank you

Berlin
  • 2,115
  • 2
  • 16
  • 28
  • local file i can play,but my streaming url has different extension for example 192.179.10.11/content/song1.apa but actual file is song1.mp4 – skyshine Jul 28 '17 at 09:38
0

heres an example of streaming video..

override func viewDidLoad() {
        super.viewDidLoad()
        let player = AVPlayer(url: URL(string: "http://techslides.com/demos/sample-videos/small.mp4")!)
        let controller = AVPlayerViewController()
        present(controller, animated: true) { _ in }
        controller.player = player
        addChildViewController(controller)
        view.addSubview(controller.view)
        controller.view.frame = CGRect(x: 50, y: 50, width: 300, height: 300)
        controller.player = player
        controller.showsPlaybackControls = true
        player.isClosedCaptionDisplayEnabled = false
        player.play()
    }
harshal jadhav
  • 5,456
  • 2
  • 18
  • 26
0

you can use YoutubePlayerView for playing youtube videos, and for else formats you can use UIWebView. For doing so, simply use if-else block,

if movieUrl.contains("youtube.com/") {
    var videoId: String = extractYoutubeId(fromLink: movieUrl)
    youtubePlayerView.load(withVideoId: videoId)
} else {  
    webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 288, height: 277))  
    webView.delegate = self
    webView.backgroundColor = UIColor.black  
    webView?.loadRequest(movieUrl)
}
Maanu
  • 945
  • 1
  • 6
  • 10
0

For Swift 3 play video from URL

//AVPlayerViewControllerDelegate // example : class TestAudioVideoVC: UIViewController,AVPlayerViewControllerDelegate

func videoPlay()
    {
        let playerController = AVPlayerViewController()
        playerController.delegate = self

        let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

        let player = AVPlayer(url: videoURL! as URL)
        playerController.player = player
        self.addChildViewController(playerController)
        self.view.addSubview(playerController.view)
        playerController.view.frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 64)
        playerController.showsPlaybackControls = true
        player.play()
    }

//MARK: - AVPlayerViewController Delegate -

    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
    }
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
0

Play video in swift 5

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func playVideoUsingURL(_ sender: Any) {
    playGlobalVideo()
}

@IBAction func playVideoFromLocalDrive(_ sender: Any) {
    playLocalVideo()
}

func playGlobalVideo() {
    guard let videoURL = URL(string: "http://static.videokart.ir/clip/100/480.mp4") else {
        return
    }
    let player = AVPlayer(url: videoURL)
    let vc = AVPlayerViewController()
    vc.player = player
    present(vc, animated: true) {
        player.play()
    }
}

func playLocalVideo() {
    guard let videoPath = Bundle.main.path(forResource: "movie", ofType: "mov") else {
        return
    }
    let player = AVPlayer(url: URL.init(fileURLWithPath: videoPath))
    let playCtr = AVPlayerViewController()
    playCtr.player = player
    present(playCtr, animated: true) {
        player.play()
    }
}

}

Mantosh Kumar
  • 275
  • 1
  • 3
  • 8