Skip to content

Commit

Permalink
Merge pull request #18 from amylaseF85/feature/play-music
Browse files Browse the repository at this point in the history
NowPlayingに曲名・アートワークを表示できるようにした
  • Loading branch information
yaplus authored Nov 1, 2019
2 parents 7aa7e71 + bfc1e0c commit 359348c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions DJYusaku.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 29WP3DF4XR;
DEVELOPMENT_TEAM = J87XZSWPMZ;
INFOPLIST_FILE = DJYusaku/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = jp.yaplus.DJYusaku;
PRODUCT_BUNDLE_IDENTIFIER = jp.yaplus.DJYusaku.amylase;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -500,13 +500,13 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 29WP3DF4XR;
DEVELOPMENT_TEAM = J87XZSWPMZ;
INFOPLIST_FILE = DJYusaku/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = jp.yaplus.DJYusaku;
PRODUCT_BUNDLE_IDENTIFIER = jp.yaplus.DJYusaku.amylase;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 5.0;
Expand Down
28 changes: 19 additions & 9 deletions DJYusaku/RequestsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RequestsViewController: UIViewController {
}

NotificationCenter.default.addObserver(self, selector: #selector(handleRequestsUpdated), name: .requestQueueToRequestsVCName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handlePlayingItemChanged), name: NSNotification.Name.MPMusicPlayerControllerNowPlayingItemDidChange, object: nil)
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -64,7 +65,23 @@ class RequestsViewController: UIViewController {
}
}

@objc func handlePlayingItemChanged(notification: NSNotification){
guard RequestQueue.shared.countRequests() > 0 else { return }
let nowPlayingMusicDataModel = RequestQueue.shared.getRequest(index: 0)
RequestQueue.shared.removeRequest(index: 0)
DispatchQueue.global().async {
let fetchedImage = Artwork.fetch(url: nowPlayingMusicDataModel.artworkUrl)
DispatchQueue.main.async {
self.tableView.reloadData()
self.playingTitle.text = nowPlayingMusicDataModel.title
self.playingArtwork.image = fetchedImage

}
}
}

@objc func handleRequestsUpdated(notification: NSNotification){
print("handleRequestsUpdated")
// リクエスト画面を更新
DispatchQueue.main.async{
self.tableView.reloadData()
Expand Down Expand Up @@ -97,10 +114,7 @@ class RequestsViewController: UIViewController {
let descripter = MPMusicPlayerStoreQueueDescriptor(storeIDs: [songID])
musicPlayerApplicationController.setQueue(with: descripter)
musicPlayerApplicationController.play()
RequestQueue.shared.removeRequest(index: 0) // TableViewには表示しない
DispatchQueue.main.async{
self.tableView.reloadData()
}

}
}

Expand All @@ -114,11 +128,7 @@ class RequestsViewController: UIViewController {
mutableQueue.remove(mutableQueue.items[0])
}, completionHandler: { [unowned self] queue, error in
guard self.musicPlayerApplicationController.nowPlayingItem != nil && error == nil else { return }
// RequestQueueと辻褄を合わせる
RequestQueue.shared.removeRequest(index: 0)
DispatchQueue.main.async{
self.tableView.reloadData()
}
// 何もしない
})
}

Expand Down

0 comments on commit 359348c

Please sign in to comment.