【问题标题】:How to play video in ios swift如何在ios swift中播放视频
【发布时间】:2017-07-28 07:00:41
【问题描述】:

我是 ios 编程新手,实际上我需要流式传输视频 URL,但我无法使用 avplayer 流式传输视频 URL,但使用 avplayer 下载的文件我可以播放。实际问题是我的文件格式不同 例如我的文件名就像这 song.apa 但它是 song.mp4

代码:

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

【问题讨论】:

  • AVPlayer 无法流式传输 Youtube 视频,请参阅此答案 stackoverflow.com/a/34709948/1518174
  • 在 youtube 中是基于在 iframe 中工作的,你不能在 avplayer 中播放。使用 webview 或 youtubeplayer
  • 不仅 youtube 视频其他视频链接我也无法播放,我更改了链接
  • 你可以举一些例子,检查你的应用程序传输安全是否被阻止

标签: ios swift avplayer


【解决方案1】:

您无法使用 AVPlayer 或 AVPlayerViewController 播放此特定链接 (http://techslides.com/demos/sample-videos/small.3gp),因为该文件采用 AMR(自适应多速率,一种语音格式) - 自 iOS 4.3 起不受支持 可以播放一些 3gp 文件,但只有那些具有 Apple 支持的视频和音频流的文件。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在 Swift 3 中,尝试使用此代码在项目中播放视频

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

      我希望它对你有用, 谢谢你

      【讨论】:

      • 我可以播放的本地文件,但我的流媒体网址有不同的扩展名,例如 192.179.10.11/content/song1.apa 但实际文件是 song1.mp4
      【解决方案4】:

      这里有一个流式视频的例子..

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

      【讨论】:

      • 我的流媒体视频 url 链接扩展名不同,例如 song1.apa 但实际格式是 song1.mp4
      • song1.apa ??从未听说过 .apa 扩展名
      • song1.app 但我的文件只有在我将该链接提供给正在运行的 android 播放器时才是 mp4
      • 文件格式是否与ios中的实际mime类型匹配有任何限制
      • 如果你的文件是 mp4 为什么 url 返回 song.apa
      【解决方案5】:

      您可以使用 YoutubePlayerView 播放 youtube 视频,对于其他格式,您可以使用 UIWebView。为此,只需使用if-else 块,

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

      【讨论】:

        【解决方案6】:

        对于 Swift 3,从 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
            }
        

        【讨论】:

          【解决方案7】:

          在 swift 5 中播放视频

          类视图控制器: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()
              }
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2017-04-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-10
            • 2017-01-30
            • 2012-01-08
            • 2017-01-13
            • 1970-01-01
            相关资源
            最近更新 更多