无法使用AVPlayer播放.mp4视频文件

时间:2017-09-11 05:17:25

标签: ios avfoundation avplayer avplayerviewcontroller avkit

我正在使用func playVideoFromUrl(urlString: String){ let videoURL = URL(string: urlString) let player = AVPlayer(url: videoURL!) let playerViewController = AVPlayerViewController() playerViewController.player = player let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate delegate.window?.rootViewController?.present(playerViewController, animated: true) { playerViewController.player!.play() } } 播放来自网址的视频。

url of video

function savedata(type) { $('#ajaxLoad').removeClass('displayNone'); $('#btnType').val(type); $('#ConfigureEmailForm').attr('action','saveTemplate.action'); $('#ConfigureEmailForm').trigger('submit'); setTimeout(function() { if(!$('.error').is(':visible')) { $("#btnActivate").attr('disabled', 'disabled'); $("#btnSave").attr('disabled', 'disabled'); self.parent.tb_remove(); self.parent.eventManagement(); self.parent.$('#EventSaveSuccess').removeClass('displayNone'); } $('#ajaxLoad').addClass('displayNone'); },500); } : - " http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4"

我在控制台没有收到任何警告。下面是模拟器的截图。我做错了什么?

enter image description here

3 个答案:

答案 0 :(得分:3)

您是否尝试在info.plist文件中添加任意加载标记,因为我看到网址是 HTTP 网址并且要使 HTTP 网址正常工作,我们需要通过info.plist在app transport security中指定它,如下所示

enter image description here

请尝试这样做,因为视频与您的代码完美配合。

阅读以下文档,了解有关App Transportation Security

的更多信息

我希望这会对你有所帮助:)。

答案 1 :(得分:3)

我已经完成了你想做的事情。这是我的方法: 首先导入AVFoundation,AVKit(我相信你有)然后我配置了这样的viewcontroller

   @IBOutlet weak var avPlayerView: UIView! // drag a UIView to view controller
   var videoPlayer = AVPlayer()
   var avPlayerViewController = AVPlayerViewController()

   override func viewDidLoad() {
     super.viewDidLoad()

     let videoURL = URL(string: "http://gsn-input-dev.s3.amazonaws.com/public/video/00229/9229d4ad2a0c547e7bfa51e3fbef806f.mp4")

    self.playVideo(url: videoURL!)

}

playVideo功能配置如下

  func playVideo(url: URL) {
       self.videoPlayer = AVPlayer(url: url)
       self.avPlayerViewController = AVPlayerViewController()
       self.avPlayerViewController.player = self.videoPlayer
       avPlayerViewController.view.frame = avPlayerView.frame
       self.addChildViewController(avPlayerViewController)
       self.view.addSubview((avPlayerViewController.view)!)

   }

将您的info.plist打开为源代码enter image description here

然后将其添加到info.plist

   <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
屏幕截图如下

enter image description here

答案 2 :(得分:0)

我最近发现,使用AVPlayer运行iOS 14的设备无法播放.mp4文件。适用于AVPlayer的兼容视频格式是.mov