LMMediaItem.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // LMMediaItem.m
  3. // iPodMusicSample
  4. //
  5. // Created by Akira Matsuda on 2014/01/27.
  6. // Copyright (c) 2014年 Akira Matsuda. All rights reserved.
  7. //
  8. #import "LMMediaItem.h"
  9. #import <MediaPlayer/MediaPlayer.h>
  10. #import <AVFoundation/AVFoundation.h>
  11. #import "LMMediaPlayerHelper.h"
  12. @interface LMMediaItem () {
  13. id metaMedia_;
  14. NSString *title_;
  15. NSString *albumTitle_;
  16. NSString *artist_;
  17. UIImage *artworkImage_;
  18. NSURL *url_;
  19. }
  20. @end
  21. @implementation LMMediaItem
  22. NSString *LMMediaItemInfoTitleKey = @"LMMediaItemInfoTitleKey";
  23. NSString *LMMediaItemInfoAlubumTitleKey = @"LMMediaItemInfoAlubumTitleKey";
  24. NSString *LMMediaItemInfoArtistKey = @"LMMediaItemInfoArtistKey";
  25. NSString *LMMediaItemInfoArtworkKey = @"LMMediaItemInfoArtworkKey";
  26. NSString *LMMediaItemInfoURLKey = @"LMMediaItemInfoURLKey";
  27. NSString *LMMediaItemInfoContentTypeKey = @"LMMediaItemInfoContentTypeKey";
  28. @synthesize title = title_;
  29. @synthesize albumTitle = albumTitle_;
  30. @synthesize artist = artist_;
  31. @synthesize assetURL = url_;
  32. - (void)dealloc
  33. {
  34. LM_RELEASE(title_);
  35. LM_RELEASE(albumTitle_);
  36. LM_RELEASE(artist_);
  37. LM_RELEASE(artworkImage_);
  38. LM_RELEASE(url_);
  39. LM_DEALLOC(super);
  40. }
  41. - (instancetype)initWithMetaMedia:(id)media contentType:(LMMediaItemContentType)type
  42. {
  43. self = [super init];
  44. if (self) {
  45. metaMedia_ = media;
  46. _contentType = type;
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithInfo:(NSDictionary *)info
  51. {
  52. self = [super init];
  53. if (self) {
  54. title_ = ([info[LMMediaItemInfoTitleKey] isKindOfClass:[NSString class]] ? [info[LMMediaItemInfoTitleKey] copy] : nil);
  55. albumTitle_ = ([info[LMMediaItemInfoAlubumTitleKey] isKindOfClass:[NSString class]] ? [info[LMMediaItemInfoAlubumTitleKey] copy] : nil);
  56. artist_ = ([info[LMMediaItemInfoArtistKey] isKindOfClass:[NSString class]] ? [info[LMMediaItemInfoArtistKey] copy] : nil);
  57. artworkImage_ = ([info[LMMediaItemInfoArtworkKey] isKindOfClass:[UIImage class]] ? [info[LMMediaItemInfoArtworkKey] copy] : nil);
  58. url_ = ([info[LMMediaItemInfoURLKey] isKindOfClass:[NSURL class]] ? [info[LMMediaItemInfoURLKey] copy] : nil);
  59. _contentType = (LMMediaItemContentType)([info[LMMediaItemInfoContentTypeKey] isKindOfClass:[NSNumber class]] ? [info[LMMediaItemInfoContentTypeKey] integerValue] : -1);
  60. }
  61. return self;
  62. }
  63. - (instancetype)initWithCoder:(NSCoder *)coder
  64. {
  65. self = [super init];
  66. if (self) {
  67. title_ = [coder decodeObjectForKey:LMMediaItemInfoTitleKey];
  68. albumTitle_ = [coder decodeObjectForKey:LMMediaItemInfoAlubumTitleKey];
  69. artist_ = [coder decodeObjectForKey:LMMediaItemInfoArtistKey];
  70. artworkImage_ = [coder decodeObjectForKey:LMMediaItemInfoArtworkKey];
  71. url_ = [coder decodeObjectForKey:LMMediaItemInfoURLKey];
  72. _contentType = (LMMediaItemContentType)[[coder decodeObjectForKey:LMMediaItemInfoContentTypeKey] integerValue];
  73. }
  74. return self;
  75. }
  76. - (void)encodeWithCoder:(NSCoder *)coder
  77. {
  78. [coder encodeObject:title_ forKey:LMMediaItemInfoTitleKey];
  79. [coder encodeObject:albumTitle_ forKey:LMMediaItemInfoAlubumTitleKey];
  80. [coder encodeObject:artist_ forKey:LMMediaItemInfoArtistKey];
  81. [coder encodeObject:artworkImage_ forKey:LMMediaItemInfoArtworkKey];
  82. [coder encodeObject:url_ forKey:LMMediaItemInfoURLKey];
  83. [coder encodeObject:[NSNumber numberWithInteger:_contentType] forKey:LMMediaItemInfoContentTypeKey];
  84. }
  85. - (id)copyWithZone:(NSZone *)zone
  86. {
  87. NSMutableDictionary *newInfo = [NSMutableDictionary new];
  88. if (self.title) {
  89. NSString *newString = [self.title copy];
  90. LM_AUTORELEASE(newString);
  91. newInfo[LMMediaItemInfoTitleKey] = newString ?: [NSNull null];
  92. }
  93. if (self.albumTitle) {
  94. NSString *newString = [self.albumTitle copy];
  95. LM_AUTORELEASE(newString);
  96. newInfo[LMMediaItemInfoAlubumTitleKey] = newString ?: [NSNull null];
  97. }
  98. if (self.artist) {
  99. NSString *newString = [self.artist copy];
  100. LM_AUTORELEASE(newString);
  101. newInfo[LMMediaItemInfoArtistKey] = newString ?: [NSNull null];
  102. }
  103. if (artworkImage_) {
  104. UIImage *newImage = [artworkImage_ copy];
  105. LM_AUTORELEASE(newImage);
  106. newInfo[LMMediaItemInfoArtworkKey] = newImage ?: [NSNull null];
  107. }
  108. if (self.assetURL) {
  109. NSURL *newURL = [self.assetURL copy];
  110. LM_AUTORELEASE(newURL);
  111. newInfo[LMMediaItemInfoURLKey] = newURL ?: [NSNull null];
  112. }
  113. newInfo[LMMediaItemInfoContentTypeKey] = [NSNumber numberWithInteger:_contentType];
  114. LMMediaItem *newObject = [[[self class] allocWithZone:zone] initWithInfo:newInfo];
  115. LM_RELEASE(newInfo);
  116. return newObject;
  117. }
  118. - (id)valueWithProperty:(NSString *)property cache:(id)cache
  119. {
  120. id returnValue = nil;
  121. if ([metaMedia_ isKindOfClass:[MPMediaItem class]]) {
  122. returnValue = cache = [metaMedia_ valueForProperty:property];
  123. }
  124. return returnValue;
  125. }
  126. - (NSString *)title
  127. {
  128. return title_ ?: [self valueWithProperty:MPMediaItemPropertyTitle cache:title_];
  129. }
  130. - (NSString *)albumTitle
  131. {
  132. return albumTitle_ ?: [self valueWithProperty:MPMediaItemPropertyAlbumTitle cache:albumTitle_];
  133. }
  134. - (NSString *)artist
  135. {
  136. return artist_ ?: [self valueWithProperty:MPMediaItemPropertyArtist cache:artist_];
  137. }
  138. - (UIImage *)artworkImageWithSize:(CGSize)size
  139. {
  140. UIImage * (^f)(id) = ^UIImage *(id metaMedia)
  141. {
  142. UIImage *image = nil;
  143. if ([metaMedia isKindOfClass:[MPMediaItem class]]) {
  144. artworkImage_ = image = [[metaMedia_ valueForProperty:MPMediaItemPropertyArtwork] imageWithSize:size];
  145. }
  146. return image;
  147. };
  148. return artworkImage_ ?: f(metaMedia_);
  149. }
  150. - (void)setArtworkImage:(UIImage *)image
  151. {
  152. artworkImage_ = [image copy];
  153. }
  154. - (NSURL *)assetURL
  155. {
  156. return url_ ?: [self valueWithProperty:MPMediaItemPropertyAssetURL cache:url_];
  157. }
  158. - (id)metaMedia
  159. {
  160. return metaMedia_;
  161. }
  162. - (BOOL)isVideo
  163. {
  164. return _contentType == LMMediaItemContentTypeVideo;
  165. }
  166. - (NSString *)description
  167. {
  168. return [@{ @"title" : title_ ?: @"nil",
  169. @"album" : albumTitle_ ?: @"nil",
  170. @"artist" : artist_ ?: @"nil",
  171. @"url" : url_ ?: @"nil",
  172. @"artwork" : artworkImage_ ?: @"nil",
  173. @"content type" : _contentType == LMMediaItemContentTypeAudio ? @"LMMediaItemContentTypeAudio" : @"LMMediaItemContentTypeVideo",
  174. @"meta media" : metaMedia_ ?: @"nil" } description];
  175. }
  176. @end