AVplayer 不播放 iPhone 库中的视频

Posted

技术标签:

【中文标题】AVplayer 不播放 iPhone 库中的视频【英文标题】:AVplayer not playing video from iPhone gallery 【发布时间】:2019-04-09 14:47:22 【问题描述】:

所以,这段代码在我之前在 iPhone 5s 上测试过的项目中运行良好,但是当我将它嵌入到我的主项目中时它不起作用,它从图库中选择视频,显示播放器,但一直在搜索视频——(赢了't play it),只有黑色的播放器屏幕,下方有搜索指示器和暂停按钮控件等。

我在输出窗口中得到的唯一错误是:

[1524:458877] 发现扩展时遇到 [discovery] 错误:Error Domain=PlugInKit Code=13 "query cancelled" UserInfo=NSLocalizedDescription=query cancelled

搜索了很多,并从 Apple 论坛上的一个答案中得出结论,它不会影响任何事情,最好被忽略。

import UIKit
import AVKit
import AVFoundation

class GalleryVideoSelectorViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate 

let ipc = UIImagePickerController()

override func viewDidLoad() 
    super.viewDidLoad()
    ipc.delegate=self


@IBAction func SelectVideo(_ sender: Any) 
    ipc.sourceType = .photoLibrary

    ipc.mediaTypes = ["public.movie"]
    present(ipc,animated:true,completion: nil)


@IBAction func PlayBackVideo(_ sender: Any) 
    if let videoURL = UserDefaults.standard.url(forKey: "LullabyURL") 
        let player = AVPlayer(url: videoURL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController,animated:true)
        
            playerViewController.player!.play()
        
    

@objc func imagePickerController(_ picker:UIImagePickerController, didFinishPickingMediaWithInfo info:[String:Any])

    ipc.dismiss(animated:true)

        var videourl = info["UIImagePickerControllerReferenceURL"] as? URL
        if let videoURL = videourl

            defaults.set(videoURL,forKey: "LullabyURL")
            let player = AVPlayer(url:videourl!)
            print (videourl!)
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            self.present(playerViewController,animated:true)
            
                playerViewController.player!.play()
            

        

    


【问题讨论】:

【参考方案1】:

这是我的委托的更新版本,实际上是导致问题的原因。

@objc func imagePickerController(_ picker:UIImagePickerController, didFinishPickingMediaWithInfo info:[UIImagePickerController.InfoKey:Any])

    ipc.dismiss(animated:true)
       let videourl = info[UIImagePickerController.InfoKey.mediaURL] as! URL
            UserDefaults.standard.set(videourl,forKey: "LullabyURL")
            let player = AVPlayer(url: videourl)
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            self.present(playerViewController,animated:true)
            
                playerViewController.player!.play()
            
    

将“didFinishPickingMediaWithInfo 信息:[UIImagePickerController.InfoKey:Any]”更改为“didFinishPickingMediaWithInfo 信息:[UIImagePickerController.InfoKey:Any]”

和 视频 URL 从 "let videourl = info["UIImagePickerControllerReferenceURL"] as?URL" 到 "let videourl = info[UIImagePickerController.InfoKey.mediaURL] as!URL"

【讨论】:

以上是关于AVplayer 不播放 iPhone 库中的视频的主要内容,如果未能解决你的问题,请参考以下文章

avplayer 不播放通过 IOS Objective-C 中的设备摄像头捕获的视频中的音频

AVPlayer 层不适合播放器视图

AVPlayer 无法在 iOS8 上播放 NSDocuments 目录中的视频

如何使用 avplayer 从文档目录播放视频文件

使用 AVPlayer 从 url 流式传输广播 - > iPhone 播放器控件不起作用

AVPlayer 不播放视频文件