From 51b617f333c29dcc9faa0ce707f8747345b0cf87 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 20 Apr 2023 14:49:23 +0200 Subject: [PATCH] Adds code for picking media from Apple Music --- LeCountdown.xcodeproj/project.pbxproj | 14 ++--- LeCountdown/Utils/AppleMusicPlayer.swift | 37 ++++------- LeCountdown/Views/ContentView.swift | 3 + .../Views/Reusable/AppleMusicPickerView.swift | 62 +++++++++++++++++++ LeCountdown/en.lproj/InfoPlist.strings | 9 +++ LeCountdown/fr.lproj/InfoPlist.strings | 1 + 6 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 LeCountdown/Views/Reusable/AppleMusicPickerView.swift create mode 100644 LeCountdown/en.lproj/InfoPlist.strings diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index 46b93df..7f595f0 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ C47C933529F01B5E00C780E2 /* FileLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4556F6A29E40B7800DEB40B /* FileLogger.swift */; }; C47C933629F01B6600C780E2 /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4556F6E29E40BED00DEB40B /* FileUtils.swift */; }; C47C933729F01B7A00C780E2 /* Codable+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4556F7029E40DCF00DEB40B /* Codable+Extensions.swift */; }; + C47C933929F13BD100C780E2 /* AppleMusicPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C47C933829F13BD100C780E2 /* AppleMusicPickerView.swift */; }; C48ECC0829DAC45900DE5A66 /* AppGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B56299FFA4F00CB4FBA /* AppGuard.swift */; }; C48ECC0929DAC47200DE5A66 /* AppGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B56299FFA4F00CB4FBA /* AppGuard.swift */; }; C498E59F298D4DEA00E90DE0 /* LiveTimerListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C498E59E298D4DEA00E90DE0 /* LiveTimerListView.swift */; }; @@ -417,6 +418,8 @@ C4742B58298411E800D5D950 /* CountdownFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountdownFormView.swift; sourceTree = ""; }; C4742B5A298414B000D5D950 /* ImageSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageSelectionView.swift; sourceTree = ""; }; C4742B5E2984205000D5D950 /* ViewModifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModifiers.swift; sourceTree = ""; }; + C47C933829F13BD100C780E2 /* AppleMusicPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleMusicPickerView.swift; sourceTree = ""; }; + C47C933C29F13DBD00C780E2 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; C498E59E298D4DEA00E90DE0 /* LiveTimerListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveTimerListView.swift; sourceTree = ""; }; C498E5A0298D543900E90DE0 /* LiveTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LiveTimer.swift; sourceTree = ""; }; C498E5A4299152B400E90DE0 /* GreenCheckmarkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GreenCheckmarkView.swift; sourceTree = ""; }; @@ -675,13 +678,6 @@ path = Nature; sourceTree = ""; }; - C42E970329E6DDF1005B1B8C /* Utils */ = { - isa = PBXGroup; - children = ( - ); - path = Utils; - sourceTree = ""; - }; C438C7CF2981216200BF3EF9 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -770,7 +766,6 @@ C438C80B2981DE2E00BF3EF9 /* Views */ = { isa = PBXGroup; children = ( - C42E970329E6DDF1005B1B8C /* Utils */, C4F8B1D3298BF686005C86A5 /* Reusable */, C4F8B1BA298AC83F005C86A5 /* Alarm */, C4F8B1B9298AC830005C86A5 /* Countdown */, @@ -941,6 +936,7 @@ C4F8B1D3298BF686005C86A5 /* Reusable */ = { isa = PBXGroup; children = ( + C47C933829F13BD100C780E2 /* AppleMusicPickerView.swift */, C42E96FA29E59E72005B1B8C /* BackgroundBlurView.swift */, C42E970129E6B32B005B1B8C /* CalendarView.swift */, C498E5A4299152B400E90DE0 /* GreenCheckmarkView.swift */, @@ -1222,6 +1218,7 @@ C415D3E229C0C0C20037B215 /* MailView.swift in Sources */, C438C80F29828B8600BF3EF9 /* ActivitiesView.swift in Sources */, C4E5D68029B8FD93008E7465 /* Store.swift in Sources */, + C47C933929F13BD100C780E2 /* AppleMusicPickerView.swift in Sources */, C4F8B187298AC234005C86A5 /* Activity+CoreDataProperties.swift in Sources */, C438C80D2982847300BF3EF9 /* CoreDataRequests.swift in Sources */, C4742B5F2984205000D5D950 /* ViewModifiers.swift in Sources */, @@ -1484,6 +1481,7 @@ isa = PBXVariantGroup; children = ( C415D3C929BF6D360037B215 /* fr */, + C47C933C29F13DBD00C780E2 /* en */, ); name = InfoPlist.strings; sourceTree = ""; diff --git a/LeCountdown/Utils/AppleMusicPlayer.swift b/LeCountdown/Utils/AppleMusicPlayer.swift index 5c00387..ec03fdc 100644 --- a/LeCountdown/Utils/AppleMusicPlayer.swift +++ b/LeCountdown/Utils/AppleMusicPlayer.swift @@ -8,31 +8,18 @@ import Foundation import MediaPlayer -@objc class AppleMusicPlayer : NSObject, MPMediaPickerControllerDelegate { +class AppleMusicPlayer { -// func play() { -// -// let musicPlayer = MPMusicPlayerApplicationController.applicationQueuePlayer -// musicPlayer.setQueue(with: .songs()) -// -// } -// -// func showMediaPicker(source: UIView) { -// let controller = MPMediaPickerController(mediaTypes: .music) -// controller.allowsPickingMultipleItems = true -// controller.popoverPresentationController?.sourceView = source -// controller.delegate = self -//// present(controller, animated: true) -// } -// -// func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { -// -// -//// let p = MPMediaItemCollection(items: [MPMediaItem]) -// -// let i = MPMediaItem() -// i. -// -// } + let mediaItemCollection: MPMediaItemCollection + + init(mediaItemCollection: MPMediaItemCollection) { + self.mediaItemCollection = mediaItemCollection + } + + func play() { + let musicPlayer = MPMusicPlayerApplicationController.applicationQueuePlayer + musicPlayer.setQueue(with: self.mediaItemCollection) + musicPlayer.play() + } } diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index 5af2202..6e2d40d 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -210,6 +210,9 @@ struct MainToolbarView: ToolbarContent { } .sheet(isPresented: self.$showStatsSheet, content: { NavigationStack { +// AppleMusicPickerView() { media in +// +// } ActivitiesView() } }) diff --git a/LeCountdown/Views/Reusable/AppleMusicPickerView.swift b/LeCountdown/Views/Reusable/AppleMusicPickerView.swift new file mode 100644 index 0000000..b4ff7dc --- /dev/null +++ b/LeCountdown/Views/Reusable/AppleMusicPickerView.swift @@ -0,0 +1,62 @@ +// +// MediaPickerView.swift +// LeCountdown +// +// Created by Laurent Morvillier on 20/04/2023. +// + +import SwiftUI +import MediaPlayer +import AVFoundation +import AVKit + +struct AppleMusicPickerView: UIViewControllerRepresentable { + + @Environment(\.presentationMode) var presentationMode + + var handler: (MPMediaItemCollection) -> () + + init(_ handler: @escaping (MPMediaItemCollection) -> Void) { + self.handler = handler + } + + class Coordinator: NSObject, UINavigationControllerDelegate, MPMediaPickerControllerDelegate { + + var parent: AppleMusicPickerView + + var handler: (MPMediaItemCollection) -> () + + init(_ parent: AppleMusicPickerView, handler: @escaping (MPMediaItemCollection) -> ()) { + self.parent = parent + self.handler = handler + } + + func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { + + self.handler(mediaItemCollection) + + mediaPicker.dismiss(animated: true, completion: nil) + } + } + + func makeCoordinator() -> Coordinator { + Coordinator(self, handler: self.handler) + } + + func makeUIViewController(context: UIViewControllerRepresentableContext) -> MPMediaPickerController { + let picker = MPMediaPickerController(mediaTypes: .music) + picker.delegate = context.coordinator + picker.allowsPickingMultipleItems = true + return picker + } + + func updateUIViewController(_ uiViewController: MPMediaPickerController, context: UIViewControllerRepresentableContext) { + } + +} + +struct MediaPickerView_Previews: PreviewProvider { + static var previews: some View { + AppleMusicPickerView { _ in } + } +} diff --git a/LeCountdown/en.lproj/InfoPlist.strings b/LeCountdown/en.lproj/InfoPlist.strings new file mode 100644 index 0000000..bcacad3 --- /dev/null +++ b/LeCountdown/en.lproj/InfoPlist.strings @@ -0,0 +1,9 @@ +/* + InfoPlist.strings + LeCountdown + + Created by Laurent Morvillier on 13/03/2023. + +*/ +"CFBundleDisplayName" = "Enchant"; +"NSAppleMusicUsageDescription" = "Play music from your Apple Music library to enchant your activities"; diff --git a/LeCountdown/fr.lproj/InfoPlist.strings b/LeCountdown/fr.lproj/InfoPlist.strings index 1c764e5..0c0ce82 100644 --- a/LeCountdown/fr.lproj/InfoPlist.strings +++ b/LeCountdown/fr.lproj/InfoPlist.strings @@ -6,3 +6,4 @@ */ "CFBundleDisplayName" = "Enchante"; +"NSAppleMusicUsageDescription" = "Jouez la musique de votre bibiliothèque Apple Musique pour enchanter vos activités";