AVplayer无法播放iPhone画廊中的视频

时间:2019-04-09 14:47:22

标签: ios swift avplayer

因此,此代码在我先前在iPhone 5s上测试过的项目中运行良好,但是当我将其嵌入主项目中时,它无法正常工作,它从图库中选择视频,显示播放器,但仍在搜索视频- (不播放),只有黑色的播放器屏幕(下面带有搜索指示符和暂停按钮控件等)。

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

  

[1524:458877]发现扩展名时遇到[发现]错误:错误域= PlugInKit代码= 13“查询已取消” UserInfo = {NSLocalizedDescription =查询已取消}

进行了大量搜索,并从苹果论坛上的答案之一得出结论,它没有任何作用,并且更好地被忽略了。

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 个答案:

答案 0 :(得分:0)

这是实际上引起问题的我的代表的更新版本。

@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 info:[UIImagePickerController.InfoKey:Any]”更改为“ didFinishPickingMediaWithInfo info:[UIImagePickerController.InfoKey:Any]”

和 视频网址从“让videourl = info [“ UIImagePickerControllerReferenceURL”] as?URL“变为“让videourl = info [UIImagePickerController.InfoKey.mediaURL] as!URL”