Swift:播放视频的AVPlayer显示此错误:[AVOutputContext]警告:AVF上下文不可用于sharedAudioPresentationContext

问题描述 投票:25回答:3

我正在尝试从捆绑目录播放视频,但出现此错误:

视频[29054:2968406] [MediaRemote] [AVOutputContext]警告:AVF上下文不适用于sharedAudioPresentationContex

这是我的实现:

import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {
    var playerVC = AVPlayerViewController()
    var playerView = AVPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.playVideo()

    }


    func playVideo() {
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as NSURL
        guard let fileURL  = documentsDirectory.appendingPathComponent("video.mp4") else{ return }
        self.playerView = AVPlayer(url:fileURL as URL)
        self.playerVC.player = playerView
        self.view.addSubview(self.playerVC.view)
        self.addChildViewController(self.playerVC)
        playerView.play()

    }

你们都知道我在做什么错吗?我将非常感谢您的帮助。

ios swift avplayer avplayerviewcontroller avplayerview
3个回答
0
投票

我对设备上存储的mp4文件有类似的问题,该问题通过在文件路径前添加“ file://”来解决。

 guard let strPath = Bundle.main.path(forResource: "demo", ofType: "mp4"), let url = URL(string: "file://\(strPath)") else {
        print("Umm, looks like an invalid URL!")
        return
    }

this post启发


0
投票

这是我的实现。对我来说效果很好。


-5
投票
import UIKit
import AVKit
import AVFoundation


class ViewController: UIViewController {
    var playerVC = AVPlayerViewController()
    var playerView = AVPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()

    }

override func viewDidAppear(_ animated: Bool) {

       let fileURL = NSURL(fileURLWithPath: "video.mp4")

        playerView = AVPlayer(url: fileURL as URL)
        playerViewController.player = playerView
        self.present(playerViewController, animated: true) {
        self.playerViewController.player?.play()
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.