diff --git a/Segno/Segno.xcodeproj/project.pbxproj b/Segno/Segno.xcodeproj/project.pbxproj index cd69079..d00d5eb 100644 --- a/Segno/Segno.xcodeproj/project.pbxproj +++ b/Segno/Segno.xcodeproj/project.pbxproj @@ -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 */; }; @@ -96,6 +97,7 @@ /* Begin PBXFileReference section */ 4F307A4529387C1100FA36A0 /* ShazamSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShazamSession.swift; sourceTree = ""; }; + 4F307A472938832900FA36A0 /* MusicSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicSession.swift; sourceTree = ""; }; 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 = ""; }; 4F31777E291BE4710019BDFC /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -437,6 +439,7 @@ children = ( 4FCAC5C1292B5C9000BF9CDD /* LoginSession.swift */, 4F307A4529387C1100FA36A0 /* ShazamSession.swift */, + 4F307A472938832900FA36A0 /* MusicSession.swift */, ); path = Session; sourceTree = ""; @@ -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 */, diff --git a/Segno/Segno/Data/Session/MusicSession.swift b/Segno/Segno/Data/Session/MusicSession.swift new file mode 100644 index 0000000..722e90f --- /dev/null +++ b/Segno/Segno/Data/Session/MusicSession.swift @@ -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(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? +}