잠금화면 플레이어

- 이미지 다운로드 Kingfisher로 변경
- 시간 표시 player에서 불러오지 않고 플레이어 화면에 표시하는 프로퍼티로 시간을 표시하도록 수정
This commit is contained in:
Yu Sung 2024-12-18 17:38:07 +09:00
parent 3c32c68b5f
commit 6c12152f81
2 changed files with 23 additions and 40 deletions

View File

@ -55,7 +55,6 @@ struct ContentDetailPlayView: View {
} else if audioContent.releaseDate == nil && !isAlertPreview || (audioContent.isActivePreview && !audioContent.contentUrl.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) { } else if audioContent.releaseDate == nil && !isAlertPreview || (audioContent.isActivePreview && !audioContent.contentUrl.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) {
Image(isPlaying() ? "btn_audio_content_pause" : isAlertPreview ? "btn_audio_content_preview_play" : "btn_audio_content_play") Image(isPlaying() ? "btn_audio_content_pause" : isAlertPreview ? "btn_audio_content_preview_play" : "btn_audio_content_play")
.onTapGesture { .onTapGesture {
ContentPlayManager.shared.stopAudio()
ContentPlayerPlayManager.shared.resetPlayer() ContentPlayerPlayManager.shared.resetPlayer()
if isPlaying() { if isPlaying() {

View File

@ -10,6 +10,8 @@ import AVKit
import MediaPlayer import MediaPlayer
import Combine import Combine
import Kingfisher
final class ContentPlayerPlayManager: NSObject, ObservableObject { final class ContentPlayerPlayManager: NSObject, ObservableObject {
static let shared = ContentPlayerPlayManager() static let shared = ContentPlayerPlayManager()
@ -261,29 +263,23 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
return return
} }
// URLSession let processor = DownsamplingImageProcessor(size: CGSize(width: 240, height: 240))
URLSession.shared.dataTask(with: url) { [weak self] data, _, error in
if let error = error { KingfisherManager.shared.retrieveImage(
print("앨범 아트 다운로드 실패: \(error.localizedDescription)") with: url,
DispatchQueue.main.async { options: [
self?.registerNowPlayingInfoCenter(with: nil) // .processor(processor),
} .scaleFactor(UIScreen.main.scale),
return .cacheOriginalImage
]
) { [weak self] result in
switch result {
case .success(let value):
self?.registerNowPlayingInfoCenter(with: value.image)
case .failure(let error):
self?.registerNowPlayingInfoCenter(with: nil)
} }
}
guard let data = data, let image = UIImage(data: data) else {
print("이미지 데이터를 가져오지 못했습니다.")
DispatchQueue.main.async {
self?.registerNowPlayingInfoCenter(with: nil) //
}
return
}
//
DispatchQueue.main.async {
self?.registerNowPlayingInfoCenter(with: image)
}
}.resume()
} }
private func registerNowPlayingInfoCenter(with albumArtImage: UIImage?) { private func registerNowPlayingInfoCenter(with albumArtImage: UIImage?) {
@ -298,13 +294,12 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork
} }
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime
if let player = player { if let player = player {
//
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = player.currentItem?.asset.duration.seconds ?? .zero
// progressBar // progressBar
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = player.rate
//
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player.currentTime().seconds
} }
center.nowPlayingInfo = nowPlayingInfo center.nowPlayingInfo = nowPlayingInfo
@ -325,25 +320,14 @@ final class ContentPlayerPlayManager: NSObject, ObservableObject {
return .success return .success
} }
center.seekForwardCommand.isEnabled = true center.seekForwardCommand.isEnabled = false
center.seekForwardCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in center.seekBackwardCommand.isEnabled = false
self.playNextContent()
return .success
}
center.seekBackwardCommand.isEnabled = true
center.seekBackwardCommand.addTarget { [unowned self] (commandEvent) -> MPRemoteCommandHandlerStatus in
self.playPreviousContent()
return .success
}
} }
private func unRegisterRemoteControlEvents() { private func unRegisterRemoteControlEvents() {
let center = MPRemoteCommandCenter.shared() let center = MPRemoteCommandCenter.shared()
center.playCommand.removeTarget(nil) center.playCommand.removeTarget(nil)
center.pauseCommand.removeTarget(nil) center.pauseCommand.removeTarget(nil)
center.skipForwardCommand.removeTarget(nil)
center.skipBackwardCommand.removeTarget(nil)
UIApplication.shared.endReceivingRemoteControlEvents() UIApplication.shared.endReceivingRemoteControlEvents()
} }
} }