잠금화면 플레이어 재생시간 동기화
This commit is contained in:
parent
c77d186ceb
commit
f909de3bfc
|
@ -4,25 +4,13 @@
|
|||
<dict>
|
||||
<key>FirebaseAppDelegateProxyEnabled</key>
|
||||
<false/>
|
||||
<key>GADApplicationIdentifier</key>
|
||||
<string>ca-app-pub-1299501215847962~3447556960</string>
|
||||
<key>NSAppTransportSecurity</key>
|
||||
<dict>
|
||||
<key>NSAllowsArbitraryLoads</key>
|
||||
<true/>
|
||||
</dict>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>gmarket_sans_bold.otf</string>
|
||||
<string>gmarket_sans_medium.otf</string>
|
||||
<string>gmarket_sans_light.otf</string>
|
||||
</array>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>audio</string>
|
||||
<string>fetch</string>
|
||||
<string>remote-notification</string>
|
||||
</array>
|
||||
<key>GADApplicationIdentifier</key>
|
||||
<string>ca-app-pub-1299501215847962~3447556960</string>
|
||||
<key>SKAdNetworkItems</key>
|
||||
<array>
|
||||
<dict>
|
||||
|
@ -222,5 +210,17 @@
|
|||
<string>3qcr597p9d.skadnetwork</string>
|
||||
</dict>
|
||||
</array>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>gmarket_sans_bold.otf</string>
|
||||
<string>gmarket_sans_medium.otf</string>
|
||||
<string>gmarket_sans_light.otf</string>
|
||||
</array>
|
||||
<key>UIBackgroundModes</key>
|
||||
<array>
|
||||
<string>audio</string>
|
||||
<string>fetch</string>
|
||||
<string>remote-notification</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
|
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
|
||||
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 registerNowPlayingInfoCenter(with albumArtImage: UIImage?) {
|
||||
let center = MPNowPlayingInfoCenter.default()
|
||||
var nowPlayingInfo = [String: Any]()
|
||||
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?) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue