-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 241220_MPNowPlayingInfoCenter,_MPRemoteCommandCenter.md
- Loading branch information
Showing
1 changed file
with
172 additions
and
0 deletions.
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
.../AriNote/TIL.docc/Swift/241220_MPNowPlayingInfoCenter,_MPRemoteCommandCenter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
# 241220 MPNowPlayingInfoCenter, MPRemoteCommandCenter | ||
|
||
์๋ฆผ์ผํฐ์ ์ฑ์์ ์ฌ์์ค์ธ ์ค๋์ค ์ ๋ณด๋ฅผ ๋์๋ณด์~ | ||
|
||
12์ 20์ผ (๊ธ) | ||
|
||
|
||
# ํ์ต๋ด์ฉ | ||
|
||
|
||
- MPNowPlayingInfoCenter๋ฅผ ์ด๋ป๊ฒ ํ์ฉํ๋๊ฑฐ์ง? | ||
- MPRemoteCommandCenter์๋ ์ด๋ค ๊ธฐ๋ฅ๋ค์ด ์์ง? | ||
- ์ฌ์์๊ฐ ์์ด '์ค์๊ฐ'์ผ๋ก ํ๊ธฐํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ์ง? | ||
- ์ด๋ฏธ์ง๋ฅผ ๋คํธ์ํฌ๋ก ๋ค์ด๋ฐ์์ ํ์ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ์ง? | ||
|
||
|
||
# ๊ณ ๋ฏผํ ์ / ํด๊ฒฐ๋ฐฉ๋ฒ | ||
|
||
## MPNowPlayingInfoCenter | ||
|
||
* ์ฑ์ด ์ฌ์ํ๋ ๋ฏธ๋์ด์ ์ ๋ณด๋ฅผ ์ค์ ํ๊ธฐ ์ํ ๊ฐ์ฒด. | ||
* `nowPlayingInfo`๋ผ๋ ํ๋กํผํฐ์ dictionary๋ฅผ ํ์ฉํ์ฌ ์๋์ ๊ฐ์ ์ ๋ณด๋ฅผ ์ค์ ํ ์ ์๋ค. | ||
|
||
|**ํค ๊ฐ**|**์ค๋ช **| | ||
|---|---| | ||
|`MPMediaItemPropertyAlbumTitle`|์จ๋ฒ์ ์ ๋ชฉ| | ||
|`MPMediaItemPropertyAlbumTrackCount`|์จ๋ฒ์ ํฌํจ๋ ์ด ํธ๋ ์| | ||
|`MPMediaItemPropertyAlbumTrackNumber`|ํ์ฌ ํธ๋์ ๋ฒํธ| | ||
|`MPMediaItemPropertyArtist`|์ํฐ์คํธ์ ์ด๋ฆ| | ||
|`MPMediaItemPropertyArtwork`|์จ๋ฒ ์ํธ์ํฌ ์ด๋ฏธ์ง. `MPMediaItemArtwork` ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํ๋ค.| | ||
|`MPMediaItemPropertyComposer`|์๊ณก๊ฐ์ ์ด๋ฆ| | ||
|`MPMediaItemPropertyDiscCount`|์จ๋ฒ์ ์ด ๋์คํฌ ์| | ||
|`MPMediaItemPropertyDiscNumber`|ํ์ฌ ๋์คํฌ์ ๋ฒํธ| | ||
|`MPMediaItemPropertyGenre`|์ฅ๋ฅด| | ||
|`MPMediaItemPropertyPlaybackDuration`|์ฌ์ ์๊ฐ(์ด ๋จ์)| | ||
|`MPMediaItemPropertyTitle`|ํธ๋์ ์ ๋ชฉ| | ||
|`MPNowPlayingInfoPropertyElapsedPlaybackTime`|ํ์ฌ ์ฌ์๋ ์๊ฐ(์ด ๋จ์)| | ||
|`MPNowPlayingInfoPropertyPlaybackRate`|์ฌ์ ์๋. ์ผ๋ฐ์ ์ผ๋ก 1.0์ ์ ์ ์ฌ์, 0.0์ ์ผ์ ์ ์ง๋ฅผ ์๋ฏธํ๋ค.| | ||
|`MPNowPlayingInfoPropertyDefaultPlaybackRate`|๊ธฐ๋ณธ ์ฌ์ ์๋| | ||
|`MPNowPlayingInfoPropertyPlaybackQueueIndex`|ํ์ฌ ์ฌ์ ์ค์ธ ํญ๋ชฉ์ ์ฌ์ ๋ชฉ๋ก ์ธ๋ฑ์ค| | ||
|`MPNowPlayingInfoPropertyPlaybackQueueCount`|์ฌ์ ๋ชฉ๋ก์ ์ด ํญ๋ชฉ ์| | ||
|`MPNowPlayingInfoPropertyChapterNumber`|ํ์ฌ ์ฌ์ ์ค์ธ ์ฑํฐ ๋ฒํธ| | ||
|`MPNowPlayingInfoPropertyChapterCount`|์ด ์ฑํฐ ์| | ||
|`MPNowPlayingInfoPropertyIsLiveStream`|ํ์ฌ ์ฌ์ ์ค์ธ ํญ๋ชฉ์ด ๋ผ์ด๋ธ ์คํธ๋ฆผ์ธ์ง ์ฌ๋ถ (`true` ๋๋ `false`)| | ||
|`MPNowPlayingInfoPropertyMediaType`|ํ์ฌ ์ฌ์ ์ค์ธ ๋ฏธ๋์ด์ ์ ํ. `MPNowPlayingInfoMediaType` ๊ฐ์ ์ฌ์ฉ.| | ||
|
||
```swift | ||
import MediaPlayer | ||
|
||
func updateNowPlayingInfo(title: String, artist: String, albumTitle: String, duration: TimeInterval, currentTime: TimeInterval, artworkImage: UIImage?) { | ||
var nowPlayingInfo: [String: Any] = [ | ||
MPMediaItemPropertyTitle: title, | ||
MPMediaItemPropertyArtist: artist, | ||
MPMediaItemPropertyAlbumTitle: albumTitle, | ||
MPMediaItemPropertyPlaybackDuration: duration, | ||
MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime, | ||
MPNowPlayingInfoPropertyPlaybackRate: 1.0 // 1.0์ ์ฌ์ ์ค, 0.0์ ์ผ์ ์ ์ง ์ํ๋ฅผ ๋ํ๋ | ||
] | ||
|
||
// ์จ๋ฒ ์ํธ์ํฌ๊ฐ ์์ ๊ฒฝ์ฐ ์ค์ | ||
if let artworkImage { | ||
let artwork = MPMediaItemArtwork(boundsSize: artworkImage.size) { size in | ||
return artworkImage | ||
} | ||
nowPlayingInfo[MPMediaItemPropertyArtwork] = artwork | ||
} | ||
|
||
// MPNowPlayingInfoCenter์ ์ ๋ณด ์ค์ | ||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo | ||
} | ||
``` | ||
|
||
## ์ด๋ฏธ์ง๋ฅผ ๋น๋๊ธฐ๋ก ๋ฐ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ | ||
|
||
Kingfisher ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํด์ ์ด๋ฏธ์ง๋ฅผ ๋น๋๊ธฐ๋ก ๋ค์ด๋ก๋ ๋ฐ์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. | ||
|
||
์ด๋ ๊ฒ ๋น๋๊ธฐ๋ก ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ ๋ฐ๋ ๊ฒฝ์ฐ์๋, nowPlayingInfo ์ค์ ๋ํ ๋งจ ๋ง์ง๋ง์ ๋น๋๊ธฐ๋ก ์ค์ ํ๊ฒ ๋๋ค. | ||
|
||
```swift | ||
import MediaPlayer | ||
import Kingfisher | ||
|
||
func fetchArtwork(from url: URL) async -> MPMediaItemArtwork? { | ||
do { | ||
let result = try await KingfisherManager.shared.retrieveImage(with: url) | ||
let artwork = MPMediaItemArtwork(boundsSize: result.image.size) { _ in | ||
return result.image | ||
} | ||
return artwork | ||
} catch { | ||
print("์ด๋ฏธ์ง ๋ค์ด๋ก๋ ์คํจ: \(error)") | ||
return nil | ||
} | ||
} | ||
``` | ||
|
||
## MPRemoteCommandCenter | ||
|
||
* ์ธ๋ถ ์ก์ธ์๋ฆฌ ๋ฐ ์์คํ ์ปจํธ๋กค์ด ์ ์กํ๋ ์๊ฒฉ ์ ์ด ์ด๋ฒคํธ์ ์๋ตํ๋ ๊ฐ์ฒด. | ||
* ์ด ๊ฐ์ฒด๋ก '์ฌ์', '์ผ์์ ์ง' ๋ฒํผ์ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ์ฑ ๋ด์์ ์์ ์ ์ฒ๋ฆฌํ ์ ์๋ค. | ||
|
||
```swift | ||
private func setupRemoteCommandHandlers() { | ||
// ์ฌ์ ๋ฒํผ ์ฒ๋ฆฌ | ||
commandCenter.playCommand.addTarget { [weak self] event in | ||
guard let self = self else { return .commandFailed } | ||
|
||
if !self.isPlaying { | ||
self.play() | ||
return .success | ||
} | ||
return .commandFailed | ||
} | ||
|
||
// ์ผ์์ ์ง ๋ฒํผ ์ฒ๋ฆฌ | ||
commandCenter.pauseCommand.addTarget { [weak self] event in | ||
guard let self = self else { return .commandFailed } | ||
|
||
if self.isPlaying { | ||
self.pause() | ||
return .success | ||
} | ||
return .commandFailed | ||
} | ||
} | ||
``` | ||
|
||
> ์ด ์ธ์๋ ๋ค์ํ ๋ช ๋ น๋ค์ด ์กด์ฌํ๋ค. | ||
|**Command**|**์ค๋ช **| | ||
|---|---| | ||
|`playCommand`|์ฌ์ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`pauseCommand`|์ผ์ ์ ์ง ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`stopCommand`|์ ์ง ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`togglePlayPauseCommand`|์ฌ์/์ผ์ ์ ์ง ํ ๊ธ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`nextTrackCommand`|๋ค์ ํธ๋ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`previousTrackCommand`|์ด์ ํธ๋ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`seekForwardCommand`|์์ผ๋ก ํ์ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`seekBackwardCommand`|๋ค๋ก ํ์ ๋ฒํผ์ด ๋๋ ธ์ ๋| | ||
|`skipForwardCommand`|์ ํด์ง ์๊ฐ๋งํผ ์์ผ๋ก ๊ฑด๋๋| | ||
|`skipBackwardCommand`|์ ํด์ง ์๊ฐ๋งํผ ๋ค๋ก ๊ฑด๋๋| | ||
|`changePlaybackRateCommand`|์ฌ์ ์๋ ๋ณ๊ฒฝ| | ||
|`changeRepeatModeCommand`|๋ฐ๋ณต ๋ชจ๋ ๋ณ๊ฒฝ| | ||
|`changeShuffleModeCommand`|์ ํ ๋ชจ๋ ๋ณ๊ฒฝ| | ||
|`rateCommand`|๋ฏธ๋์ด ํ๊ฐ(๋ณ์ ๋ฑ)| | ||
|`likeCommand`|์ข์์ ๋ฒํผ| | ||
|`dislikeCommand`|์ซ์ด์ ๋ฒํผ| | ||
|`bookmarkCommand`|๋ถ๋งํฌ ์ถ๊ฐ ๋ฒํผ| | ||
|
||
## ์ฌ์์ด ๋ชจ๋ ๋๋ฌ์ ๋ ์๋ฆผ ๋ฐ๋ ๋ฐฉ๋ฒ | ||
|
||
์๋์ฒ๋ผ NotificationCenter๋ฅผ ํ์ฉํด์ AVPlayerItemDidPlayToEndTime์ ๊ตฌ๋ ํ๋ฉด ๋๋ค! | ||
|
||
```swift | ||
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, | ||
object: item, | ||
queue: .main, | ||
using: { _ in | ||
// ์ฌ๊ธฐ์ ์ฑ ๋ด์์ ์ฌ์์ค์ธ AVPlayer์ ์ฌ์์ ๋ฉ์ถ๋๋ก ๊ตฌํํด๋ณด์. | ||
}) | ||
``` | ||
|
||
|
||
--- | ||
|
||
|
||
# ์ฐธ๊ณ ๋งํฌ | ||
|
||
|
||
- [https://developer.apple.com/documentation/mediaplayer/mpnowplayinginfocenter](https://developer.apple.com/documentation/mediaplayer/mpnowplayinginfocenter) | ||
- [https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter](https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter) | ||
- [https://developer.apple.com/documentation/avfoundation/avplayeritem/didplaytoendtimenotification](https://developer.apple.com/documentation/avfoundation/avplayeritem/didplaytoendtimenotification) |