【问题标题】:AVplayer not playing video from iPhone galleryAVplayer 不播放 iPhone 库中的视频
【发布时间】: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()
            }

        }

    }

}

【问题讨论】:

    标签: ios swift avplayer


    【解决方案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"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2016-10-30
      • 1970-01-01
      相关资源
      最近更新 更多