-
Notifications
You must be signed in to change notification settings - Fork 1
/
MediaPlayer+Convenience.m
214 lines (156 loc) · 6.94 KB
/
MediaPlayer+Convenience.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// MediaPlayer+Convenience.m
// Ringtonic
//
// Created by Alexander Ivanov on 15.11.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "MediaPlayer+Convenience.h"
@implementation MPMediaItem (Convenience)
- (NSUInteger)year {
NSUInteger year = [[self valueForProperty:@"year"] unsignedIntegerValue];
return year ? year : [self.releaseDate componentValue:NSCalendarUnitYear];
}
@end
@implementation MPMediaItemArtwork (Convenience)
- (UIImage *)image {
UIScreen *screen = [UIScreen mainScreen];
CGFloat size = fmax(screen.bounds.size.width, screen.bounds.size.height);
return [self imageWithSize:CGSizeMake(size, size)];
}
- (void)fetchImage:(void(^)(UIImage *image))handler {
if (!handler)
return;
id catalog = sel_(self, tryGetValueForKey:, @"_catalog");
id token = sel_(catalog, tryGetValueForKey:, @"_token");
id url = sel_(token, tryGetValueForKey:, @"_fetchableArtworkToken");
[[NSURL URLWithString:url] cache:^(NSURL *cachedURL) {
handler([UIImage imageWithContentsOfURL:cachedURL]);
}];
}
@end
@implementation MPMediaQuery (Convenience)
+ (instancetype)createWithComparisonType:(MPMediaPredicateComparison)comparisonType predicates:(NSDictionary *)predicates {
NSMutableSet *set = [NSMutableSet set];
for (NSString *key in predicates)
[set addObject:[MPMediaPropertyPredicate predicateWithValue:predicates[key] forProperty:key comparisonType:comparisonType]];
return [[self alloc] initWithFilterPredicates:set];
}
+ (instancetype)createWithComparisonType:(MPMediaPredicateComparison)comparisonType artist:(NSString *)artist album:(NSString *)album title:(NSString *)title {
NSMutableDictionary *predicates = [NSMutableDictionary dictionaryWithCapacity:3];
if (artist.length)
predicates[MPMediaItemPropertyArtist] = artist;
if (album.length)
predicates[MPMediaItemPropertyAlbumTitle] = album;
if (title.length)
predicates[MPMediaItemPropertyTitle] = title;
return [self createWithComparisonType:comparisonType predicates:predicates];
}
@end
@implementation MPMusicPlayerController (Convenience)
- (BOOL)isPlaying {
return self.playbackState == MPMusicPlaybackStatePlaying;
}
- (void)playWithCompletionHandler:(void (^)(BOOL))completionHandler {
[self prepareToPlayWithCompletionHandler:^(NSError * _Nullable error) {
[self play];
if (completionHandler)
completionHandler(error == Nil);
[error log:@"prepareToPlayWithCompletionHandler:"];
}];
}
- (void)playItem:(MPMediaItem *)item startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime completionHandler:(void (^)(BOOL))completionHandler {
if (!item)
return;
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:@[ item ]];
MPMusicPlayerMediaItemQueueDescriptor *descriptor = [[MPMusicPlayerMediaItemQueueDescriptor alloc] initWithItemCollection:collection];
if (startTime)
[descriptor setStartTime:startTime forItem:item];
if (endTime)
[descriptor setEndTime:endTime forItem:item];
[self setQueueWithDescriptor:descriptor];
[self prepareToPlayWithCompletionHandler:^(NSError * _Nullable error) {
self.currentPlaybackTime = startTime;
[self play];
if (completionHandler)
completionHandler(error == Nil);
[error log:@"prepareToPlayWithCompletionHandler:"];
}];
}
- (void)playStoreID:(NSString *)storeID startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime completionHandler:(void (^)(MPMediaItem *))completionHandler {
if (!storeID)
return;
/*
self.nowPlayingItem = Nil;
[self setQueueWithStoreIDs:@[ storeID ]];
[self prepareToPlay];
for (NSUInteger index = 0; index < 10 && !self.nowPlayingItem; index++)
[NSThread sleepForTimeInterval:0.1];
self.currentPlaybackTime = startTime;
[self play];
*/
if (@available(iOS 10.1, *)) {
MPMusicPlayerStoreQueueDescriptor *descriptor = [[MPMusicPlayerStoreQueueDescriptor alloc] initWithStoreIDs:@[ storeID ]];
if (startTime)
[descriptor setStartTime:startTime forItemWithStoreID:storeID];
if (endTime)
[descriptor setEndTime:endTime forItemWithStoreID:storeID];
[self setQueueWithDescriptor:descriptor];
[self prepareToPlayWithCompletionHandler:^(NSError * _Nullable error) {
self.currentPlaybackTime = startTime;
[self play];
if (completionHandler)
completionHandler(self.nowPlayingItem);
[error log:@"prepareToPlayWithCompletionHandler:"];
}];
}
}
- (void)beginGeneratingPlaybackNotificationsForObserver:(id)observer selector:(SEL)selector {
if (observer && selector)
[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self];
[self beginGeneratingPlaybackNotifications];
}
- (void)endGeneratingPlaybackNotificationsForObserver:(id)observer {
[self endGeneratingPlaybackNotifications];
if (observer)
[[NSNotificationCenter defaultCenter] removeObserver:observer name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:self];
}
@end
@implementation MPMediaPicker
- (void)performCompletion:(MPMediaItemCollection *)mediaItemCollection {
__weak MPMediaPicker *__self = self;
if (self.completion)
self.completion(__self, mediaItemCollection);
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
[self dismissViewControllerAnimated:YES completion:Nil];
[self performCompletion:mediaItemCollection];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[self dismissViewControllerAnimated:YES completion:Nil];
[self performCompletion:Nil];
}
- (instancetype)initWithMediaTypes:(MPMediaType)mediaTypes {
self = [super initWithMediaTypes:mediaTypes];
if (self)
self.delegate = self;
return self;
}
@end
@implementation UIViewController (MPMediaPickerController)
- (MPMediaPicker *)presentMediaPicker:(MPMediaType)types showsCloudItems:(BOOL)showsCloudItems completion:(void(^)(MPMediaPicker *sender, MPMediaItemCollection *items))completion from:(id)source {
MPMediaPicker *picker = [[MPMediaPicker alloc] initWithMediaTypes:types];
picker.completion = completion;
picker.showsCloudItems = showsCloudItems;
return [self popoverViewController:picker from:source] ? picker : Nil;
}
- (MPMediaPickerController *)presentMediaPicker:(MPMediaType)types completion:(void(^)(MPMediaPickerController *sender, MPMediaItemCollection *items))completion fromView:(UIView *)source {
return [self presentMediaPicker:types showsCloudItems:NO completion:completion from:source];
}
- (MPMediaPickerController *)presentMediaPicker:(MPMediaType)types completion:(void(^)(MPMediaPickerController *sender, MPMediaItemCollection *items))completion fromButton:(UIBarButtonItem *)source {
return [self presentMediaPicker:types showsCloudItems:NO completion:completion from:source];
}
- (MPMediaPickerController *)presentMediaPicker:(MPMediaType)types completion:(void(^)(MPMediaPickerController *sender, MPMediaItemCollection *items))completion {
return [self presentMediaPicker:types showsCloudItems:NO completion:completion from:Nil];
}
@end