使用按钮在xcode 10中播放视频

问题描述 投票:0回答:1

说实话,我不太明白我在这里做的是我试图用来播放它构建的视频的代码,但是当我按下按钮时,我得到一个“sigabrt”警告并且它不起作用。

这是代码:

import UIKit

import AVKit

import AVFoundation

    class ViewController: UIViewController {


        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

        }
        @IBAction func VideoPlayer(_ sender: Any) {


                guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
                    debugPrint("video.m4v not found")
                    return
                }
                let player = AVPlayer(url: URL(fileURLWithPath: path))
                let playerController = AVPlayerViewController()
                playerController.player = player
                present(playerController, animated: true) {
                    player.play()
                }
            }
        }


}
button video xcode10
1个回答
0
投票

试试这个。

import UIKit
import AVKit

class ViewController: UIViewController {

    var avPlayer: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func VideoPlayer(_ sender: Any) {
          playVideo()
     }

    func playVideo() {

        let filepath: String? = Bundle.main.path(forResource: "videoplayback", ofType: "mp4")
        let fileURL = URL.init(fileURLWithPath: filepath!)

        avPlayer = AVPlayer(url: fileURL)
        let avPlayerController = AVPlayerViewController()
        avPlayerController.player = avPlayer
        avPlayerController.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)

        // Turn on video controlls
        avPlayerController.showsPlaybackControls = true

        // play video
        avPlayerController.player?.play()
        self.view.addSubview(avPlayerController.view)
        self.addChild(avPlayerController)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.