gpt4 book ai didi

ios - 如何让我的视频背景继续使用 swift 播放?

转载 作者:可可西里 更新时间:2023-11-01 02:17:06 25 4
gpt4 key购买 nike

我有一个视频作为我的着陆 View Controller 的背景播放,每当我退出应用程序然后重新进入时,视频会卡住,直到我转到另一个 View 并返回,或者只是重新启动应用程序它正常播放。如何让它在应用程序退出并重新进入时继续播放?

逻辑是这样的:

import UIKit
import AVFoundation
class FirstViewController: UIViewController {

var player: AVPlayer?

override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("sunny", ofType: "mp4")
player = AVPlayer(URL: NSURL(fileURLWithPath: path!))
player!.actionAtItemEnd = AVPlayerActionAtItemEnd.None;
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
playerLayer.videoGravity = AVLayerVideoGravityResize
self.view.layer.insertSublayer(playerLayer, atIndex: 0)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.playerItemDidReachEnd), name: AVPlayerItemDidPlayToEndTimeNotification, object: player!.currentItem)
player!.seekToTime(kCMTimeZero)
player!.play()

}

func playerItemDidReachEnd() {
player!.seekToTime(kCMTimeZero)
}

}

最佳答案

首先,您需要在文件 VideoCutter.swift 中创建一个名为 VideoCutter 的类:

import UIKit
import AVFoundation

extension String {
var convert: NSString { return (self as NSString) }
}

public class VideoCutter: NSObject {
public func cropVideoWithUrl(videoUrl url: NSURL, startTime: CGFloat, duration: CGFloat, completion: ((videoPath: NSURL?, error: NSError?) -> Void)?) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let asset = AVURLAsset(URL: url, options: nil)
let exportSession = AVAssetExportSession(asset: asset, presetName: "AVAssetExportPresetHighestQuality")
let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var outputURL = paths.objectAtIndex(0) as! String
let manager = NSFileManager.defaultManager()
do {
try manager.createDirectoryAtPath(outputURL, withIntermediateDirectories: true, attributes: nil)
} catch _ {
}
outputURL = outputURL.convert.stringByAppendingPathComponent("sunny.mp4")
do {
try manager.removeItemAtPath(outputURL)
} catch _ {
}
if let exportSession = exportSession as AVAssetExportSession? {
exportSession.outputURL = NSURL(fileURLWithPath: outputURL)
exportSession.shouldOptimizeForNetworkUse = true
exportSession.outputFileType = AVFileTypeMPEG4
let start = CMTimeMakeWithSeconds(Float64(startTime), 600)
let duration = CMTimeMakeWithSeconds(Float64(duration), 600)
let range = CMTimeRangeMake(start, duration)
exportSession.timeRange = range
exportSession.exportAsynchronouslyWithCompletionHandler { () -> Void in
switch exportSession.status {
case AVAssetExportSessionStatus.Completed:
completion?(videoPath: exportSession.outputURL, error: nil)
case AVAssetExportSessionStatus.Failed:
print("Failed: \(exportSession.error)")
case AVAssetExportSessionStatus.Cancelled:
print("Failed: \(exportSession.error)")
default:
print("default case")
}
}
}
dispatch_async(dispatch_get_main_queue()) {
}
}
}
}

然后你需要在VideoSplashViewController.swift文件中创建一个类VideoSplashViewController:

import UIKit
import MediaPlayer
import AVKit

public enum ScalingMode {
case Resize
case ResizeAspect
case ResizeAspectFill
}

public class VideoSplashViewController: UIViewController {

private let moviePlayer = AVPlayerViewController()
private var moviePlayerSoundLevel: Float = 1.0
public var contentURL: NSURL = NSURL() {
didSet {
setMoviePlayer(contentURL)
}
}

public var videoFrame: CGRect = CGRect()
public var startTime: CGFloat = 0.0
public var duration: CGFloat = 0.0
public var backgroundColor: UIColor = UIColor.blackColor() {
didSet {
view.backgroundColor = backgroundColor
}
}
public var sound: Bool = true {
didSet {
if sound {
moviePlayerSoundLevel = 1.0
}else{
moviePlayerSoundLevel = 0.0
}
}
}
public var alpha: CGFloat = CGFloat() {
didSet {
moviePlayer.view.alpha = alpha
}
}
public var alwaysRepeat: Bool = true {
didSet {
if alwaysRepeat {
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: moviePlayer.player?.currentItem)
}
}
}
public var fillMode: ScalingMode = .ResizeAspectFill {
didSet {
switch fillMode {
case .Resize:
moviePlayer.videoGravity = AVLayerVideoGravityResize
case .ResizeAspect:
moviePlayer.videoGravity = AVLayerVideoGravityResizeAspect
case .ResizeAspectFill:
moviePlayer.videoGravity = AVLayerVideoGravityResizeAspectFill
}
}
}

override public func viewDidAppear(animated: Bool) {
moviePlayer.view.frame = videoFrame
moviePlayer.showsPlaybackControls = false
view.addSubview(moviePlayer.view)
view.sendSubviewToBack(moviePlayer.view)
}

override public func viewWillDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}

private func setMoviePlayer(url: NSURL){
let videoCutter = VideoCutter()
videoCutter.cropVideoWithUrl(videoUrl: url, startTime: startTime, duration: duration) { (videoPath, error) -> Void in
if let path = videoPath as NSURL? {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
dispatch_async(dispatch_get_main_queue()) {
self.moviePlayer.player = AVPlayer(URL: path)
self.moviePlayer.player?.play()
self.moviePlayer.player?.volume = self.moviePlayerSoundLevel
}
}
}
}
}

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

override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func playerItemDidReachEnd() {
moviePlayer.player?.seekToTime(kCMTimeZero)
moviePlayer.player?.play()
}
}

最后,在您的 FirstViewController 中:

import UIKit

class FirstViewController: VideoSplashViewController {

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

func setupVideoBackground() {

let url = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("sunny", ofType: "mp4")!)

videoFrame = view.frame
fillMode = .ResizeAspectFill
alwaysRepeat = true
sound = true
startTime = 2.0
alpha = 0.8

contentURL = url
view.userInteractionEnabled = false

}
}

关于ios - 如何让我的视频背景继续使用 swift 播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36684165/

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