从后台转换到活动状态后,如何恢复 mp4 视频? Xcode 10.1,Swift 4.2
How can I resume the mp4 video after transition from the background to the active state? Xcode 10.1, Swift 4.2
我正在 UIView
中展示视频。我添加了一个通知功能,可以在视频停止后一次又一次地播放视频。但是让我们假设用户在 App 中观看视频时单击主页按钮。之后让我们假设用户再次打开应用程序。(从后台过渡到活动状态)
在这种情况下,视频不播放。从后台转换到活动状态后,如何恢复 mp4 视频?我想我应该在 AppDelegate.swift 中使用 applicationWillEnterForeground
方法,对吗?但是怎么办?你能为此编写所需的代码吗?
ViewController.swift 文件:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var videoViewOutlet: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
private func setupView()
{
let pathClouds = URL(fileURLWithPath: Bundle.main.path(forResource: "clouds", ofType: "mp4")!)
let player = AVPlayer(url: pathClouds)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoViewOutlet.frame
self.videoViewOutlet.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player.play()
//loop it
player.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.videoDidPlayToEnd(notification:)),
name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: player.currentItem)
}
@objc func videoDidPlayToEnd(notification: Notification)
{
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: CMTime.zero)
}
}
这是 AppDelegate.swift 文件
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
// ??????
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
您可以通过侦听 ViewController 中的相关通知,根据应用的背景/前景暂停/恢复它。这是一个例子。
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var videoViewOutlet: UIView!
private var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
private func setupView()
{
let pathClouds = URL(fileURLWithPath: Bundle.main.path(forResource: "clouds", ofType: "mp4")!)
player = AVPlayer(url: pathClouds)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoViewOutlet.frame
self.videoViewOutlet.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player.play()
//loop it
player.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.videoDidPlayToEnd(notification:)),
name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: player.currentItem)
NotificationCenter.default.addObserver(self, selector: #selector(enteredBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(enteredForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func videoDidPlayToEnd(notification: Notification)
{
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: .zero, completionHandler: nil)
}
@objc func enteredBackground() {
player.pause()
}
@objc func enteredForeground() {
player.play()
}
}
我正在 UIView
中展示视频。我添加了一个通知功能,可以在视频停止后一次又一次地播放视频。但是让我们假设用户在 App 中观看视频时单击主页按钮。之后让我们假设用户再次打开应用程序。(从后台过渡到活动状态)
在这种情况下,视频不播放。从后台转换到活动状态后,如何恢复 mp4 视频?我想我应该在 AppDelegate.swift 中使用 applicationWillEnterForeground
方法,对吗?但是怎么办?你能为此编写所需的代码吗?
ViewController.swift 文件:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var videoViewOutlet: UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
private func setupView()
{
let pathClouds = URL(fileURLWithPath: Bundle.main.path(forResource: "clouds", ofType: "mp4")!)
let player = AVPlayer(url: pathClouds)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoViewOutlet.frame
self.videoViewOutlet.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player.play()
//loop it
player.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.videoDidPlayToEnd(notification:)),
name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: player.currentItem)
}
@objc func videoDidPlayToEnd(notification: Notification)
{
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: CMTime.zero)
}
}
这是 AppDelegate.swift 文件
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
// ??????
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
您可以通过侦听 ViewController 中的相关通知,根据应用的背景/前景暂停/恢复它。这是一个例子。
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var videoViewOutlet: UIView!
private var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
self.setupView()
}
private func setupView()
{
let pathClouds = URL(fileURLWithPath: Bundle.main.path(forResource: "clouds", ofType: "mp4")!)
player = AVPlayer(url: pathClouds)
let newLayer = AVPlayerLayer(player: player)
newLayer.frame = self.videoViewOutlet.frame
self.videoViewOutlet.layer.addSublayer(newLayer)
newLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
player.play()
//loop it
player.actionAtItemEnd = .none
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.videoDidPlayToEnd(notification:)),
name: NSNotification.Name(rawValue: "AVPlayerItemDidPlayToEndTimeNotification"), object: player.currentItem)
NotificationCenter.default.addObserver(self, selector: #selector(enteredBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(enteredForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func videoDidPlayToEnd(notification: Notification)
{
let player: AVPlayerItem = notification.object as! AVPlayerItem
player.seek(to: .zero, completionHandler: nil)
}
@objc func enteredBackground() {
player.pause()
}
@objc func enteredForeground() {
player.play()
}
}