Skip to content

Commit

Permalink
#142 feat: MusicSession 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantchoi committed Dec 1, 2022
1 parent 7ff6308 commit 89232a2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Segno/Segno.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
4F307A4629387C1100FA36A0 /* ShazamSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F307A4529387C1100FA36A0 /* ShazamSession.swift */; };
4F307A482938832900FA36A0 /* MusicSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F307A472938832900FA36A0 /* MusicSession.swift */; };
4F31777F291BE4710019BDFC /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F31777E291BE4710019BDFC /* SceneDelegate.swift */; };
4F317786291BE4720019BDFC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4F317785291BE4720019BDFC /* Assets.xcassets */; };
4F317789291BE4720019BDFC /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4F317787291BE4720019BDFC /* LaunchScreen.storyboard */; };
Expand Down Expand Up @@ -96,6 +97,7 @@

/* Begin PBXFileReference section */
4F307A4529387C1100FA36A0 /* ShazamSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShazamSession.swift; sourceTree = "<group>"; };
4F307A472938832900FA36A0 /* MusicSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicSession.swift; sourceTree = "<group>"; };
4F317779291BE4710019BDFC /* Segno.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Segno.app; sourceTree = BUILT_PRODUCTS_DIR; };
4F31777C291BE4710019BDFC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
4F31777E291BE4710019BDFC /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +439,7 @@
children = (
4FCAC5C1292B5C9000BF9CDD /* LoginSession.swift */,
4F307A4529387C1100FA36A0 /* ShazamSession.swift */,
4F307A472938832900FA36A0 /* MusicSession.swift */,
);
path = Session;
sourceTree = "<group>";
Expand Down Expand Up @@ -562,6 +565,7 @@
983AE9D82935CEE2006547BD /* SettingsViewModel.swift in Sources */,
9825F41D29377ACF005F2163 /* SettingsRepository.swift in Sources */,
66F0D7EE2925FF8B0074872E /* DiaryCell.swift in Sources */,
4F307A482938832900FA36A0 /* MusicSession.swift in Sources */,
4F9A00202922337F007D9057 /* LoginViewController.swift in Sources */,
982A2A472924AE74006F6ACD /* UserDefaultsKey.swift in Sources */,
66A8CF6B2937947A00C17F84 /* UserDetailDTO.swift in Sources */,
Expand Down
79 changes: 79 additions & 0 deletions Segno/Segno/Data/Session/MusicSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// MusicSession.swift
// Segno
//
// Created by Gordon Choi on 2022/12/01.
//

import MusicKit

final class MusicSession {
private var songInfo: SongInfo?
private var song: Song?

private lazy var player = ApplicationMusicPlayer.shared
private lazy var playerState = player.state

private var isPlaying: Bool {
return playerState.playbackStatus == .playing
}

private let request: MusicCatalogSearchRequest = {
var request = MusicCatalogSearchRequest(term: "Happy", types: [Song.self])
request.limit = 1
return request
}()

func fetchMusic(term: SongInfo?) {
guard let term else { return }

Task {
let status = await MusicAuthorization.request()
switch status {
case .authorized:
do {
let request = MusicCatalogResourceRequest<Song>(matching: \.isrc, equalTo: term.isrc)
let response = try await request.response()
if let item = response.items.first {
song = item
songInfo = SongInfo(isrc: item.isrc!, title: item.title, artist: item.artistName, album: item.albumTitle)
}

print(songInfo ?? "NO SONG")
} catch (let error) {
print(error.localizedDescription)
}
default:
debugPrint("no")
}
}
}

// 음악을 재생하는 함수
func playMusic() {
guard let song else { return }
if !isPlaying {
player.queue = [song]

Task {
do {
try await player.play()
} catch let error {
print(error.localizedDescription)
}
}
} else {
player.pause()
}

// 뷰 컨트롤러를 나갈 때 큐를 비워 준다.
// 뮤직세션, 샤잠세션은 싱글턴 인스턴스로 만들어 주는 것이 좋겠다.
}
}

struct SongInfo {
let isrc: String
let title: String
let artist: String
let album: String?
}

0 comments on commit 89232a2

Please sign in to comment.