Skip to content

Commit

Permalink
Create 241220_MPNowPlayingInfoCenter,_MPRemoteCommandCenter.md
Browse files Browse the repository at this point in the history
  • Loading branch information
leeari95 committed Dec 22, 2024
1 parent 5b87f9a commit 000ef24
Showing 1 changed file with 172 additions and 0 deletions.
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)

0 comments on commit 000ef24

Please sign in to comment.