잠금화면 플레이어 재생시간 동기화

This commit is contained in:
Yu Sung
2024-12-18 22:02:41 +09:00
parent c77d186ceb
commit f909de3bfc
2 changed files with 272 additions and 239 deletions

View File

@@ -46,11 +46,21 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
override init() {
self.player = AVPlayer()
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback, mode: .moviePlayback)
try audioSession.setActive(true)
} catch {
DEBUG_LOG("Audio Session 설정 실패: \(error.localizedDescription)")
}
super.init()
}
func setPlaylist(playlist: [AudioContentPlaylistContent]) {
resetPlayer()
self.registerRemoteControlEvents()
self.playlist = playlist
playlistManager = AudioContentPlaylistManager(playlist: playlist)
playNextContent()
@@ -70,7 +80,6 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
.sink { [weak self] currentTime in
if !(self?.isEditing ?? false) {
self?.currentTime = CMTimeGetSeconds(currentTime)
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = self?.currentTime
}
}
.store(in: &cancellables)
@@ -110,16 +119,7 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
}
.store(in: &cancellables)
do {
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback, mode: .moviePlayback)
try audioSession.setActive(true)
self.fetchAlbumArtAndUpdateNowPlayingInfo()
self.registerRemoteControlEvents()
} catch {
DEBUG_LOG("Audio Session 설정 실패: \(error.localizedDescription)")
}
self.fetchAlbumArtAndUpdateNowPlayingInfo()
}
private func checkPlaybackStart(bufferedTime: Double, isLikelyToKeepUp: Bool) {
@@ -138,6 +138,7 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
DEBUG_LOG("재생 중단: 버퍼링 부족 (\(bufferedTime)초)")
}
}
updateNowPlayingInfo()
}
private func handlePlaybackEnded() {
@@ -283,10 +284,24 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
}
}
private func updateNowPlayingInfo() {
guard var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo else { return }
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime().seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = isPlaying ? 1.0 : 0.0
let duration = CMTimeGetSeconds(player?.currentItem?.duration ?? CMTime.zero)
if duration.isFinite {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
}
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
private func registerNowPlayingInfoCenter(with albumArtImage: UIImage?) {
let center = MPNowPlayingInfoCenter.default()
var nowPlayingInfo = [String: Any]()
guard let currentItem = player?.currentItem else { return }
var nowPlayingInfo = [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = title
nowPlayingInfo[MPMediaItemPropertyArtist] = nickname
@@ -296,33 +311,51 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
}
if let player = player {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.currentItem?.duration.seconds
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
// CMTimeGetSeconds duration
let duration = CMTimeGetSeconds(currentItem.duration)
if duration.isFinite {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
}
}
center.nowPlayingInfo = nowPlayingInfo
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
private func registerRemoteControlEvents() {
let center = MPRemoteCommandCenter.shared()
center.playCommand.isEnabled = true
center.playCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in
self.playOrPause()
return .success
}
center.pauseCommand.isEnabled = true
center.pauseCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in
self.playOrPause()
return .success
}
center.nextTrackCommand.isEnabled = true
center.nextTrackCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in
self.playNextContent()
return .success
}
center.previousTrackCommand.isEnabled = true
center.previousTrackCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in
self.playPreviousContent()
return .success
}
}
private func unRegisterRemoteControlEvents() {
let center = MPRemoteCommandCenter.shared()
center.playCommand.removeTarget(nil)
center.pauseCommand.removeTarget(nil)
center.nextTrackCommand.removeTarget(nil)
center.previousTrackCommand.removeTarget(nil)
UIApplication.shared.endReceivingRemoteControlEvents()
}
}