gpt4 book ai didi

swift - 创建一个观察者来检查 MediaPlayer playbackState 是否暂停

转载 作者:行者123 更新时间:2023-11-28 14:12:00 26 4
gpt4 key购买 nike

我有一个音乐应用程序,我想确定在应用程序关闭时播放是否已暂停(由于诸如电话或 AirPods 从耳朵中取出等事件)

我的第一个方法是在 viewWillAppear 中运行一个函数来检查

if mediaPlayer.playbackState == .paused {
...
}

如果暂停,我会更新播放/暂停按钮图像。但是,这不起作用,即使暂停,播放/暂停按钮仍会显示播放。

接下来,我尝试将观察者添加到 viewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(self.wasSongInterupted(_:)), name: UIApplication.didBecomeActiveNotification, object: self.mediaPlayer)

我调用的self.wasSongInterupted

@objc func wasSongInterupted(_ notification: Notification) {
DispatchQueue.main.async {
if self.mediaPlayer.playbackState == .paused {
print("paused")
self.isPlaying = false
self.playPauseSongButton.isSelected = self.isPlaying
} else if self.mediaPlayer.playbackState == .playing {
self.isPlaying = true
self.playPauseSongButton.isSelected = self.isPlaying
}
}
}

但是,我仍然遇到同样的问题。

当我重新打开应用程序时,确定我的音乐播放器是正在播放还是暂停的最佳方法是什么?

编辑 1:我根据评论编辑了我的代码。

wasSongInterrupted 没有被调用,通过断点和错误我发现大部分代码是不需要的。我将代码更改为

func wasSongInterrupted() {
DispatchQueue.main.async {
if self.mediaPlayer.playbackState == .interrupted {
var isPlaying: Bool { return self.mediaPlayer.playbackState == .playing }
print("Playback state is \(self.mediaPlayer.playbackState.rawValue), self.isPlaying Bool is \(self.isPlaying)")
self.playPauseSongButton.setImage(UIImage(named: "playIconLight"), for: .normal)
//self.playPauseSongButton.isSelected = self.isPlaying
}
}
}

在我的 AppDelegate 的 applicationDidBecomeActive 中,我有

let mediaPlayerVC = MediaPlayerViewController()
mediaPlayerVC.wasSongInterupted()

现在代码可以运行了,但是我遇到了问题。

如果我运行以下代码:

if self.mediaPlayer.playbackState == .interrupted {
print("interrupted \(self.isPlaying)")
}

然后调用并返回到应用程序,它会遇到断点。它将打印出 interrupted 和 false,这是 self.isPlaying

的 Bool 值

但是,如果我尝试通过

更新 UI
self.playPauseSongButton.isSelected = self.isPlaying

或通过

self.playPauseSongButton.setImage(UIImage(named: "playIconLight.png"), for: .normal)

我收到一条错误消息 Thread 1: EXC_BREAKPOINT (code=1, subcode=0x104af9258)

最佳答案

您正尝试从 viewWillAppear 更新您的播放器 UI。来自 Apple Documentation :

viewWillAppear(_:)

This method is called before the view controller's view is about to be added to a view hierarchy and before any animations are configured for showing the view.

因此,如果您的应用程序被暂停并且再次变为事件,则不会调用此方法,因为您的UIViewController 已经在导航堆栈

如果你想捕捉你的应用从suspended状态变为active的时刻,你需要使用AppDelegate。来自 Apple Documentation :

applicationDidBecomeActive(_:)

This method is called to let your app know that it moved from the inactive to active state. This can occur because your app was launched by the user or the system.

因此您需要在您的AppDelegate 中使用此方法来处理应用程序运行并更新您的界面。

更新

你说的是你正在做的 AppDelegate 方法里面

let mediaPlayerVC = MediaPlayerViewController()
mediaPlayerVC.wasSongInterupted()

这是错误的,因为您正在创建一个新的 View Controller 。您需要做的是从导航堆栈访问您的现有 View Controller 并更新它。

可能的解决方案之一是使用 NotificationCenter 发送通知。您的 View Controller 当然应该订阅此事件。

首先,您需要创建一个通知名称

extension Notification.Name {
static let appBecameActive = Notification.Name(rawValue: "appBecameActive")
}

然后在您的 AppDelegate 中添加以下代码以在应用激活时发布您的通知

func applicationDidBecomeActive(_ application: UIApplication) {
NotificationCenter.default.post(name: .appBecameActive, object: nil)
}

最后在您的 View Controller 中添加订阅通知

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self,
selector: #selector(wakeUp),
name: .appBecameActive,
object: nil)
...
}

@objc func wakeUp() {
// Update your UI from here
}

希望对你有帮助。

关于swift - 创建一个观察者来检查 MediaPlayer playbackState 是否暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52431292/

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