gpt4 book ai didi

ios - 在 applicationDidBecomeActive 时恢复 AVPlayer 视频?

转载 作者:行者123 更新时间:2023-11-28 06:19:54 26 4
gpt4 key购买 nike

目前正在使用 AVPlayer 在 View 中播放视频作为我的背景,但是当我返回主屏幕然后重新打开应用程序时,视频已暂停并且我无法继续播放。当然关闭应用程序并重新打开修复它,但我真的需要它在用户返回到应用程序时恢复。
我尝试在 AppDelegate 中使用 applicationDidBecomeActive,方法是创建一个链接到我的 viewcontroller 的变量,然后调用 avPlayer.play()。然而,这随后会返回一个错误,说 found nil。我还尝试在 applicationDidBecomeActive 中调用 viewDidLoad() ,然后给我一个 BAD INSTRUCTION 错误,即使代码在不从委托(delegate)中调用它时工作得很好。
如有任何帮助,我们将不胜感激!

View Controller :

import UIKit
import AVFoundation


class ViewController: UIViewController, CLLocationManagerDelegate {

var avPlayer: AVPlayer!
var avPlayerLayer: AVPlayerLayer!
var paused: Bool = false
var foregroundNotification: NSObjectProtocol!

//viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector:#selector(ViewController.viewDidLoad), name:NSNotification.Name.UIApplicationWillEnterForeground, object:UIApplication.shared)


//Rotate icon

let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotateAnimation.fromValue = 0.0
rotateAnimation.toValue = 360.0
rotateAnimation.repeatCount = HUGE
rotateAnimation.duration = 450


checkInButton.layer.add(rotateAnimation, forKey: "myAnimationKey");

//Play background video

let theURL = Bundle.main.url(forResource:"city2", withExtension: "mp4")

avPlayer = AVPlayer(url: theURL!)
avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
avPlayer.volume = 0
avPlayer.actionAtItemEnd = .none
//avPlayer.rate = 1

avPlayerLayer.frame = view.layer.bounds
view.backgroundColor = .clear
view.layer.insertSublayer(avPlayerLayer, at: 0)

NotificationCenter.default.addObserver(self,
selector: #selector(playerItemDidReachEnd(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: avPlayer.currentItem)

}


func playerItemDidReachEnd(notification: Notification) {
let p: AVPlayerItem = notification.object as! AVPlayerItem
p.seek(to: kCMTimeZero)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
avPlayer.play()
paused = false

}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
avPlayer.pause()
paused = true

}
}

代表:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var vc = ViewController()


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.

vc.avPlayer.play()
vc.paused = false

}

最佳答案

像这样设置一次通知。

NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)

然后在这里处理你播放的音乐

func appMovedToForeground() {
//do you play stuff here
}

关于ios - 在 applicationDidBecomeActive 时恢复 AVPlayer 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43944226/

26 4 0
文章推荐: java - 在 C++ 中正确表示 Java 的 ArrayList