gpt4 book ai didi

ios - Swift 4 和 Xcode 10。在应用启动时播放视频,完成后显示 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:24 24 4
gpt4 key购买 nike

首先,我是 Xcode 10 和 Swift 4 的新手,我在这里搜索过但没有找到有效的代码。

我追求的是:启动应用程序播放本地存储的视频(称为“launchvideo”)。视频完成后显示/移动到 Storyboard ID 为“菜单”的 UIviewcontroller

到目前为止,我有我的主导航 Controller 及其链接 View Controller 。我猜我需要一个 UIview 来保存要在此页面上播放的视频?

有人可以帮助新人吗?谢谢

最佳答案

首先从“常规”选项卡的项目设置中将启动屏幕 Storyboard更改为“主 Storyboard”。

创建一个具有以下名称的 View Controller 并编写代码来实现 AVPlayer 播放视频。

import UIKit
import AVFoundation

class VideoLaunchVC: UIViewController {

func setupAVPlayer() {

let videoURL = Bundle.main.url(forResource: "Video", withExtension: "mov") // Get video url
let avAssets = AVAsset(url: videoURL!) // Create assets to get duration of video.
let avPlayer = AVPlayer(url: videoURL!) // Create avPlayer instance
let avPlayerLayer = AVPlayerLayer(player: avPlayer) // Create avPlayerLayer instance
avPlayerLayer.frame = self.view.bounds // Set bounds of avPlayerLayer
self.view.layer.addSublayer(avPlayerLayer) // Add avPlayerLayer to view's layer.
avPlayer.play() // Play video

// Add observer for every second to check video completed or not,
// If video play is completed then redirect to desire view controller.
avPlayer.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: 1) , queue: .main) { [weak self] time in

if time == avAssets.duration {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
self?.navigationController?.pushViewController(vc, animated: true)
}
}
}

//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.setupAVPlayer() // Call method to setup AVPlayer & AVPlayerLayer to play video
}
}

主要 Storyboard: Video Launch VC

项目启动屏幕文件: Project Launch Screen File

另见以下视频:

https://youtu.be/dvi0JKEpNTc

关于ios - Swift 4 和 Xcode 10。在应用启动时播放视频,完成后显示 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54642211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com