使用计时器连续播放2个视频

时间:2016-08-22 22:47:02

标签: swift video timer avplayer swift3

我希望使用计时器播放另一个视频后播放视频(第一个视频长度为414秒)。

我遇到的问题:

- 它取代了视频2,它只是显示了视频1的最后一帧。我可以听到视频2的音频,但只能看到视频1的最后一帧。加上 - 视频框架适用于视频1,但是当视频2开始时全部搞砸了。我知道有一种更简单的方法让视频适合屏幕尺寸 - 任何帮助都会受到赞赏。

以下是我目前正在使用的内容:

import AVFoundation
import AVKit

var player = AVPlayer()
let playerViewController = AVPlayerViewController()
var player2 = AVPlayer()
let playerViewController2 = AVPlayerViewController()
var countTimer = Timer()
var count = Double()
var moviePath = Bundle.main.path(forResource: "Video1", ofType: "m4v")

override func viewDidLoad() {
 playVideo()
 countTimer = Timer(timeInterval: 0.01, target: self, selector: #selector(countFunc), userInfo: nil, repeats: true)
 RunLoop.current.add(countTimer, forMode: RunLoopMode.commonModes)

func countFunc()
{
    count += 0.01
    if(count > 414.0 && count < 414.1)
    {
        playVideo2()
    }


func playVideo()
{

    moviePath = Bundle.main.path(forResource: "Video1", ofType: "m4v")

        let url = URL(fileURLWithPath: moviePath!)
        player = AVPlayer(url: url)

        playerViewController.player = player

        playerViewController.view.frame = CGRect(x: self.view.frame.width * ((-146)/(374)), y: (self.view.frame.height * (146)/(667 * 1.014)), width: self.view.frame.height, height: self.view.frame.width * 1.01)
        playerViewController.showsPlaybackControls = false
        UIView.animate(withDuration: 0.001, animations: {
            self.playerViewController.view.transform = CGAffineTransform(rotationAngle: -CGFloat(M_PI)/2)

        })
        self.view.addSubview(playerViewController.view)
        self.player.play()

}

func playVideo2()
{
    print("Video2")
    playerViewController.view.removeFromSuperview()
    player.pause()

    moviePath = Bundle.main.path(forResource: "Video2", ofType: "m4v")

    let url = URL(fileURLWithPath: moviePath!)
    player2 = AVPlayer(url: url)

    playerViewController2.player = player

    playerViewController2.view.frame = CGRect(x: self.view.frame.width * ((-219)/(561)), y: (self.view.frame.height * (219)/(1000.5 * 1.521)), width: self.view.frame.height, height: self.view.frame.width * 1.515)
    playerViewController2.showsPlaybackControls = false
    UIView.animate(withDuration: 0.001, animations: {
        self.playerViewController2.view.transform = CGAffineTransform(rotationAngle: -CGFloat(M_PI)/2)

    })
    self.view.addSubview(playerViewController2.view)
    self.player2.play()
}

0 个答案:

没有答案