【问题标题】:How do I Add A GIF/Video To Landing Screen Background In Xcode 6 using swift?如何使用 swift 在 Xcode 6 中将 GIF/视频添加到登陆屏幕背景?
【发布时间】:2015-05-29 11:30:37
【问题描述】:

在 Xcode 中将 GIF/视频添加到我的应用程序的登陆屏幕(主屏幕或第一个视图控制器)背景的最有效方法是什么?即像 spotify、uber、insagram 等应用程序。由于我的应用程序是通用的,我将如何使其适合?

【问题讨论】:

  • 自动布局是通用应用的解决方案。但是,您的问题还不清楚。提供更多细节。
  • 检查我的编辑@SaqibOmer
  • 什么是视频源?从本地存储播放的一些网址?
  • 不是个人视频,从我的 iPhone 导入到我的 macbook @SaqibOmer

标签: ios swift video xcode6 gif


【解决方案1】:

您是指应用启动后显示的第一个屏幕吗?如果是这样:很遗憾,您不能拥有动态内容;您将无法使用 gif/视频。

也就是说,如果您在后台线程上有一些应用程序设置,无论如何都需要一些时间,或者您只是希望用户在交互之前等待更长时间以便您可以显示 gif/视频,那么您可以做什么?可以使静态图像匹配 gif/video 的第一帧,并让您的入口点成为显示实际 gif/video 的 ViewController。但是,因为这会延迟交互时间,所以不建议这样做。

至于使其适合:从 iOS 8 开始 Apple recommends using LaunchScreen.xib。有了它,你可以使用 Auto Layout 来实现通用性。

要添加视频,您可以使用 MPMoviePlayerController、AVPlayer,或者如果您使用的是 SPritekit,您可以使用 SKVideoNode。

编辑(响应后续 cmets):

NSURL 是对本地或远程文件的引用。 This link 会给你一个不错的概述。只需复制电影并按照该指南进行操作即可。

除了 Saqib Omer 建议的 MPMoviePlayerController 解决方案之外,这里还有一种使用带有 AVPlayerLayer 的 UIView 的替代方法。例如,它在视频顶部有一个按钮,因为这就是您要查找的内容。

import AVKit
import AVFoundation
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Start with a generic UIView and add it to the ViewController view
        let myPlayerView = UIView(frame: self.view.bounds)
        myPlayerView.backgroundColor = UIColor.blackColor()
        view.addSubview(myPlayerView)

        // Use a local or remote URL
        let url = NSURL(string: "http://eoimages.gsfc.nasa.gov/images/imagerecords/76000/76740/iss030-e-6082_sdtv.mov") // See the note on NSURL above.

        // Make a player
        let myPlayer = AVPlayer(URL: url)
        myPlayer.play()

        // Make the AVPlayerLayer and add it to myPlayerView's layer
        let avLayer = AVPlayerLayer(player: myPlayer)
        avLayer.frame = myPlayerView.bounds
        myPlayerView.layer.addSublayer(avLayer)

        // Make a button and add it to myPlayerView (you'd need to add an action, of course)
        let myButtonOrigin = CGPoint(x: myPlayerView.bounds.size.width / 3, y: myPlayerView.bounds.size.height / 2)
        let myButtonSize = CGSize(width: myPlayerView.bounds.size.width / 3, height: myPlayerView.bounds.size.height / 10)
        let myButton = UIButton(frame: CGRect(origin: myButtonOrigin, size: myButtonSize))
        myButton.setTitle("Press Me!", forState: .Normal)
        myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        myPlayerView.addSubview(myButton)
    }
}

【讨论】:

  • 不是启动图像/屏幕,实际的第一个交互屏幕。我指的主屏幕。即像 spotify、instagram、uber 等应用程序。他们有视频在后台运行,而他们有签名 uo 按钮、登录按钮等可供用户访问
  • 感谢它在模拟器中运行得非常好,但我不希望播放的视频依赖于互联网访问,我希望它播放不管这就是我想要创建路径的原因对于我自己的本地磁盘上的个人视频,而不是网站上的网址。我还不清楚。如果可能的话,我也希望它循环播放
  • 查看我在回答中添加的关于 NSURL 的链接——你不需要依赖网络,你可以使用本地文件。对于按钮操作和循环,您应该提出一个新问题(嗯,两个新问题),或者在线查看一些教程。这些问题应该只关注一个问题。你会到达那里的!
【解决方案2】:

为了播放视频添加以下代码,声明类变量var moviePlayer:MPMoviePlayerController!。比在你的viewDidLoad()

var url:NSURL = NSURL(string: "YOUR_URL_FOR_VIDEO")



moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 0, y: 0, width: 200, height: 150)



 self.view.addSubview(moviePlayer.view)
  moviePlayer.fullscreen = true



moviePlayer.controlStyle = MPMovieControlStyle.Embedded

这将播放视频。但要适应它,您需要添加布局约束。请参阅this 链接以务实地添加约束。

【讨论】:

  • 如果我的 iPhone 上的我个人收藏的视频导入到我的 Mac 中,URL 会是“nil”吗? MPMoviePlayer 已经在 swift 库中了吗? @saqib omer
  • 这里有一个很好的教程,告诉你你想要什么。 medium.com/swift-programming/…
【解决方案3】:
import MediaPlayer

class ViewController: UIViewController {

    var moviePlayer: MPMoviePlayerController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load the video from the app bundle.
        let videoURL: NSURL = NSBundle.mainBundle().URLForResource("video", withExtension: "mp4")!

        // Create and configure the movie player.
        self.moviePlayer = MPMoviePlayerController(contentURL: videoURL)

        self.moviePlayer.controlStyle = MPMovieControlStyle.None
        self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill

        self.moviePlayer.view.frame = self.view.frame
        self.view .insertSubview(self.moviePlayer.view, atIndex: 0)

        self.moviePlayer.play()

        // Loop video.
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loopVideo", name: MPMoviePlayerPlaybackDidFinishNotification, object: self.moviePlayer)
    }

    func loopVideo() {
        self.moviePlayer.play()
    }

https://medium.com/@kschaller/ios-video-backgrounds-6eead788f190#.2fhxmc2da

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多