UGCKitBGMListViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitBGMListViewController.h"
  3. #import "UGCKitBGMHelper.h"
  4. #import "UGCKitBGMCell.h"
  5. #import <MediaPlayer/MediaPlayer.h>
  6. #import <AVFoundation/AVFoundation.h>
  7. #import "UGCKitColorMacro.h"
  8. #import <objc/runtime.h>
  9. #import "UGCKitMem.h"
  10. @interface UGCKitBGMListViewController()<TCBGMHelperListener,UGCKitBGMCellDelegate,MPMediaPickerControllerDelegate>{
  11. NSMutableDictionary* _progressList;
  12. NSTimeInterval lastUIFreshTick;
  13. UGCKitTheme *_theme;
  14. }
  15. @property(nonatomic,strong) NSDictionary* bgmDict;
  16. @property(nonatomic,strong) NSArray* bgmKeys;
  17. @property(nonatomic,strong) UGCKitBGMHelper* bgmHelper;
  18. @property(nonatomic,weak) id<TCBGMControllerListener> bgmListener;
  19. @end
  20. @implementation UGCKitBGMListViewController
  21. {
  22. NSIndexPath *_BGMCellPath;
  23. BOOL _useLocalMusic;
  24. }
  25. - (instancetype)initWithTheme:(UGCKitTheme *)theme {
  26. if (self = [self initWithStyle:UITableViewStylePlain]) {
  27. _theme = theme;
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithStyle:(UITableViewStyle)style
  32. {
  33. self = [super initWithStyle:style];
  34. if (self) {
  35. _progressList = [NSMutableDictionary new];
  36. _useLocalMusic = NO;
  37. }
  38. return self;
  39. }
  40. -(void)setBGMControllerListener:(id<TCBGMControllerListener>) listener{
  41. _bgmListener = listener;
  42. }
  43. -(void)viewWillAppear:(BOOL)animated
  44. {
  45. [super viewWillAppear:animated];
  46. }
  47. -(void)viewDidLoad{
  48. [super viewDidLoad];
  49. self.title = [_theme localizedString:@"UGCKit.BGMListView.TitileChooseBGM"];
  50. UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithImage:_theme.backIcon style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
  51. self.navigationItem.leftBarButtonItem = customBackButton;
  52. self.tableView.backgroundColor = RGB(25, 29, 38);
  53. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  54. NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"UGCKitResources" ofType:@"bundle"];
  55. NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
  56. [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UGCKitBGMCell class]) bundle:bundle] forCellReuseIdentifier:@"UGCKitBGMCell"];
  57. }
  58. - (void)goBack
  59. {
  60. [_bgmListener onBGMControllerPlay:nil];
  61. }
  62. - (void)loadBGMList{
  63. if (_useLocalMusic) {
  64. [self showMPMediaPickerController];
  65. }else{
  66. lastUIFreshTick = [[NSDate date] timeIntervalSince1970]*1000;
  67. _bgmHelper = [UGCKitBGMHelper sharedInstance];
  68. [_bgmHelper setDelegate:self];
  69. NSString* jsonUrl = @"http://bgm-1252463788.cosgz.myqcloud.com/bgm_list.json";
  70. [_bgmHelper initBGMListWithJsonFile:jsonUrl];
  71. }
  72. }
  73. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  74. {
  75. return 1;
  76. }
  77. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  78. {
  79. return [_bgmKeys count];
  80. }
  81. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. return 80;
  84. }
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. UGCKitBGMCell* cell = (UGCKitBGMCell *)[tableView dequeueReusableCellWithIdentifier:@"UGCKitBGMCell"];
  88. if (!cell) {
  89. cell = [[UGCKitBGMCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UGCKitBGMCell"];
  90. }
  91. if (nil == cell.downloadButtonBackground) {
  92. cell.downloadButtonBackground = _theme.recordMusicDownloadIcon;
  93. cell.progressButtonBackground = _theme.nextIcon;
  94. cell.downloadText = [_theme localizedString:@"UGCKit.Common.Download"];
  95. cell.downloadingText = [_theme localizedString:@"UGCKit.UGCKit.Common.Downloading"];
  96. cell.applyText = [_theme localizedString:@"UGCKit.Common.Apply"];
  97. }
  98. cell.delegate = self;
  99. TCBGMElement* ele = _bgmDict[_bgmKeys[indexPath.row]];
  100. if (ele.localUrl) {
  101. [cell setDownloadProgress:1.0];
  102. cell.progressView.hidden = YES;
  103. }else{
  104. cell.progressView.hidden = YES;
  105. [cell.downLoadBtn setTitle:[_theme localizedString:@"UGCKit.Common.Download"] forState:UIControlStateNormal];
  106. }
  107. cell.downLoadBtn.hidden = [_BGMCellPath isEqual:indexPath];
  108. cell.musicLabel.text = ele.name;
  109. return cell;
  110. }
  111. - (void)onBGMDownLoad:(UGCKitBGMCell *)cell;
  112. {
  113. if (_BGMCellPath) {
  114. UGCKitBGMCell *cell = (UGCKitBGMCell*)[self.tableView cellForRowAtIndexPath:_BGMCellPath];
  115. cell.progressView.hidden = YES;
  116. cell.downLoadBtn.hidden = NO;
  117. }
  118. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  119. _BGMCellPath = indexPath;
  120. cell.downLoadBtn.hidden = YES;
  121. cell.progressView.hidden = NO;
  122. TCBGMElement* ele = _bgmDict[_bgmKeys[indexPath.row]];
  123. if([ele isValid] && [[NSFileManager defaultManager] fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:[ele localUrl]]]){
  124. [_bgmListener onBGMControllerPlay: [NSHomeDirectory() stringByAppendingPathComponent:[ele localUrl]]];
  125. }
  126. else [_bgmHelper downloadBGM: _bgmDict[_bgmKeys[indexPath.row]]];
  127. }
  128. -(void) onBGMListLoad:(NSDictionary*)dict{
  129. BOOL foundKeyBGMToLoadRemote = NO;
  130. if(dict){
  131. BGMLog(ASLocalizedString(@"BGM List 加载成功"));
  132. _bgmDict = dict;
  133. _bgmKeys = [_bgmDict keysSortedByValueUsingComparator:^(TCBGMElement* e1, TCBGMElement* e2){
  134. return [[e1 name] compare:[e2 name]];
  135. }];
  136. for (NSString* url in _bgmKeys) {
  137. TCBGMElement* ele = [_bgmDict objectForKey:url];
  138. if([[ele isValid] boolValue]){
  139. @synchronized (_progressList) {
  140. [_progressList setObject :[NSNumber numberWithFloat:1.f] forKey:url];
  141. }
  142. }
  143. // 没有青花瓷时用本地音乐,AppStore审核用
  144. NSRange range = [ele.name rangeOfString:ASLocalizedString(@"青花瓷")]; //
  145. if (range.location != NSNotFound) {
  146. foundKeyBGMToLoadRemote = YES;
  147. }
  148. }
  149. }
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. if (foundKeyBGMToLoadRemote) {
  152. self->_useLocalMusic = NO;
  153. [self.tableView reloadData];
  154. }else{
  155. self->_useLocalMusic = YES;
  156. [self showMPMediaPickerController];
  157. }
  158. });
  159. }
  160. -(void) onBGMDownloading:(TCBGMElement*)current percent:(float)percent{
  161. dispatch_async(dispatch_get_main_queue(), ^{
  162. if ([[self.tableView indexPathsForVisibleRows] containsObject:self->_BGMCellPath]) {
  163. UGCKitBGMCell *cell = [self.tableView cellForRowAtIndexPath:self->_BGMCellPath];
  164. cell.progressView.hidden = NO;
  165. cell.downLoadBtn.hidden = YES;
  166. [cell setDownloadProgress:percent];
  167. }
  168. });
  169. }
  170. -(void) onBGMDownloadDone:(TCBGMElement*)element{
  171. if([[element isValid] boolValue]){
  172. BGMLog(@"Download \"%@\" success!", [element name]);
  173. @synchronized (_progressList) {
  174. [_progressList setObject :[NSNumber numberWithFloat:1.f] forKey:[element netUrl]];
  175. }
  176. WEAKIFY(self);
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. STRONGIFY_OR_RETURN(self);
  179. UGCKitBGMCell *cell = [self.tableView cellForRowAtIndexPath:self->_BGMCellPath];
  180. cell.progressView.hidden = YES;
  181. cell.downLoadBtn.hidden = NO;
  182. self->_BGMCellPath = nil;
  183. [self->_bgmListener onBGMControllerPlay: [NSHomeDirectory() stringByAppendingPathComponent:[element localUrl]]];
  184. });
  185. }
  186. else BGMLog(@"Download \"%@\" failed!", [element name]);
  187. }
  188. static void *mpcKey = &mpcKey;
  189. - (void)showMPMediaPickerController
  190. {
  191. MPMediaPickerController *mpc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
  192. mpc.delegate = self;
  193. mpc.editing = YES;
  194. mpc.allowsPickingMultipleItems = NO;
  195. mpc.showsCloudItems = NO;
  196. if (@available(iOS 9.2, *)) {
  197. mpc.showsItemsWithProtectedAssets = NO;
  198. }
  199. mpc.modalPresentationStyle = UIModalPresentationFullScreen;
  200. objc_setAssociatedObject(mpc, mpcKey, self, OBJC_ASSOCIATION_RETAIN);
  201. UINavigationController *nav = self.navigationController;
  202. self.navigationController.navigationBar.hidden = YES;
  203. [self.navigationController setViewControllers:@[mpc] animated:NO];
  204. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  205. }
  206. #pragma mark - BGM
  207. //选中后调用
  208. - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
  209. NSArray *items = mediaItemCollection.items;
  210. MPMediaItem *songItem = [items objectAtIndex:0];
  211. NSURL *url = [songItem valueForProperty:MPMediaItemPropertyAssetURL];
  212. AVAsset *songAsset = [AVAsset assetWithURL:url];
  213. if (songAsset != nil) {
  214. [_bgmListener onBGMControllerPlay:songAsset];
  215. }
  216. }
  217. //点击取消时回调
  218. - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{
  219. [_bgmListener onBGMControllerPlay:nil];
  220. }
  221. @end