Browse Source

使用音乐事件传递,音乐下载成功

Chris 5 years ago
parent
commit
c7f4fbea87

+ 57 - 1
RainbowPlanet/RainbowPlanet/Modules/PublishModule/AliyunVideo/AlivcShortVideo/ShortVideoFile/VideoEdit/Controller/AliyunEditViewController.m

@@ -27,6 +27,7 @@
 #import "MBProgressHUD.h"
 #import "NSString+AlivcHelper.h"
 #import "UIView+AlivcHelper.h"
+#import "AFNetworking.h"
 
 //公用类相关
 #import "AliyunDBHelper.h"
@@ -57,6 +58,9 @@ typedef enum : NSUInteger {
 // TODO:此类需再抽一层,否则会太庞大
 @interface AliyunEditViewController () <
 AliyunIExporterCallback, AliyunIPlayerCallback, AliyunMusicPickViewControllerDelegate>
+{
+    NSURLSessionDownloadTask *_downloadTask;
+}
 
 @property(nonatomic, strong) UIView *movieView;
 @property(nonatomic, strong) UIButton *backgroundTouchButton;
@@ -605,6 +609,7 @@ AliyunIExporterCallback, AliyunIPlayerCallback, AliyunMusicPickViewControllerDel
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resourceDeleteNoti:) name:AliyunEffectResourceDeleteNotification object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadMusicAndCombineNoti:) name:@"DownloadMusicAndCombineNoti" object:nil];
     
 }
 
@@ -613,6 +618,13 @@ AliyunIExporterCallback, AliyunIPlayerCallback, AliyunMusicPickViewControllerDel
 }
 
 #pragma mark - Notification Action
+- (void)downloadMusicAndCombineNoti:(NSNotification *)noti {
+    NSString *urlStr = noti.object;
+    NSLog(@"\n------receiveNoti.urlStr == %@", urlStr);
+    [self downloadFileFromServerWithUrlStr:urlStr];
+}
+
+
 //资源删除通知
 - (void)resourceDeleteNoti:(NSNotification *)noti {
     NSArray *deleteResourcePaths = noti.object;
@@ -651,7 +663,51 @@ AliyunIExporterCallback, AliyunIPlayerCallback, AliyunMusicPickViewControllerDel
     }
 }
 
-#pragma mark - Common Method
+#pragma mark - Download Music Method
+- (void)downloadFileFromServerWithUrlStr:(NSString *)urlStr{
+    
+    NSURL *URL = [NSURL URLWithString:urlStr];
+    
+    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
+    
+    //AFN3.0+基于封住URLSession的句柄
+    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
+    
+    //请求
+    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
+    
+    //下载Task操作
+    _downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
+        
+        // @property int64_t totalUnitCount;  需要下载文件的总大小
+        // @property int64_t completedUnitCount; 当前已经下载的大小
+        
+        // 给Progress添加监听 KVO
+        NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
+        // 回到主队列刷新UI
+//        dispatch_async(dispatch_get_main_queue(), ^{
+//            self.progressView.progress = 1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount;
+//        });
+        
+    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
+        
+        //- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径
+        
+        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
+        NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
+        return [NSURL fileURLWithPath:path];
+        
+    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
+        // filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用
+        
+        NSString *musicFilePath = [filePath path];// 将NSURL转成NSString
+        NSLog(@"\n------下载音乐成功!path == %@", musicFilePath);
+//        /var/mobile/Containers/Data/Application/8E7DC39B-199C-4D88-A2CC-E1E45BEC61AD/Library/Caches/FndRYcJTMSbC1N2UXur3tJSeH_CF_67_60_028.mp3
+    }];
+    
+    // 开始执行下载
+    [_downloadTask resume];
+}
 
 #pragma mark - Play Manager
 

+ 9 - 15
RainbowPlanet/RainbowPlanet/Modules/PublishModule/PublishMusicAbout/PublishMusicChooseView.swift

@@ -125,19 +125,6 @@ class PublishMusicChooseView: FWPopupView {
     func setupSegmentedView() {
         addSubview(segmentedView)
         addSubview(listContainerView)
-//        reloadData()
-    }
-    
-    /// 刷新页面
-    func reloadData() {
-        segmentedDataSource.titles = categoryTitleArr
-        segmentedView.indicators = [indicator]
-        
-        
-        //        self.segmentedDataSource.reloadData(selectedIndex: shoppingMallListVCType)
-        //        self.segmentedView.defaultSelectedIndex = shoppingMallListVCType
-        self.segmentedView.reloadData()
-        
     }
     
     // MARK: - 视图创建
@@ -334,12 +321,19 @@ extension PublishMusicChooseView :JXSegmentedListContainerViewDataSource {
         } else {
             listVc.categoryItemMdl = categoryListMdlArr![index-1]
         }
-        listVc.selMusicClosure = {
-            [weak self] (musicUrl) in
+        listVc.playMusicClosure = {
+            (musicUrl) in
             MusicPlayManager.shared().initPlay()
             MusicPlayManager.shared().audioUrl = musicUrl
             MusicPlayManager.shared().playAudio()
         }
+        listVc.chooseMusicClosure = {
+            [weak self] (musicUrl) in
+            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DownloadMusicAndCombineNoti"), object: musicUrl)
+            MusicPlayManager.shared().destroyPlayer()
+            MusicPlayManager.shared().curPlayingId = -1
+            self?.hide()
+        }
         return listVc
     }
 }

+ 10 - 5
RainbowPlanet/RainbowPlanet/Modules/PublishModule/PublishMusicAbout/PublishMusicListController.swift

@@ -17,8 +17,11 @@ class PublishMusicListController: BaseViewController {
         listViewDidScrollCallback = nil
     }
     
-    typealias SelMusicClosure = (_ musicUrl: String) -> Void
-    var selMusicClosure : SelMusicClosure?
+    typealias PlayMusicClosure = (_ musicUrl: String) -> Void
+    var playMusicClosure : PlayMusicClosure?
+    
+    typealias ChooseMusicClosure = (_ musicUrl: String) -> Void
+    var chooseMusicClosure : ChooseMusicClosure?
     
     // 推荐下の音乐
     var isReccomendList: Bool? {
@@ -89,15 +92,17 @@ extension PublishMusicListController : UITableViewDelegate, UITableViewDataSourc
         cell.useClickClosure = {
             [weak self] in
             let musicUrlStr = self?.musicItemMdlArr[indexPath.row].url
-            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DownloadMusicAndCombineNoti"), object: musicUrlStr)
+            if let chooseMusicClosure = self?.chooseMusicClosure {
+                chooseMusicClosure(musicUrlStr ?? "")
+            }
         }
         return cell
     }
     
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let musicUrl = musicItemMdlArr[indexPath.row].url!
-        if let selMusicClosure = self.selMusicClosure {
-            selMusicClosure(musicUrl)
+        if let playMusicClosure = self.playMusicClosure {
+            playMusicClosure(musicUrl)
         }
         
         let musicId = musicItemMdlArr[indexPath.row].id!