Pārlūkot izejas kodu

音乐播放管理,播放状态显示

Chris 5 gadi atpakaļ
vecāks
revīzija
f7f518d094

+ 12 - 0
RainbowPlanet/RainbowPlanet.xcodeproj/project.pbxproj

@@ -484,6 +484,7 @@
 		BD10FC0222C6F9770096A34E /* AlivcShortVideoUploadManager.m in Sources */ = {isa = PBXBuildFile; fileRef = BD10FC0022C6F9770096A34E /* AlivcShortVideoUploadManager.m */; };
 		BD10FC0522C6F9880096A34E /* AliyunSVideoApi.m in Sources */ = {isa = PBXBuildFile; fileRef = BD10FC0322C6F9870096A34E /* AliyunSVideoApi.m */; };
 		BD10FC0822C748DA0096A34E /* AlivcAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = BD10FC0622C748D90096A34E /* AlivcAlertView.m */; };
+		BD11D64222E02520008FFA58 /* MusicPlayManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD11D64122E02520008FFA58 /* MusicPlayManager.swift */; };
 		BD12203622AF807C0051C7C2 /* MessagePlanetActivityController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD12203522AF807C0051C7C2 /* MessagePlanetActivityController.swift */; };
 		BD12203A22AF80AD0051C7C2 /* MessageActivityTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD12203922AF80AD0051C7C2 /* MessageActivityTableCell.swift */; };
 		BD12203F22AF8E190051C7C2 /* MessagePlanetNotiController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD12203E22AF8E190051C7C2 /* MessagePlanetNotiController.swift */; };
@@ -1214,6 +1215,7 @@
 		BD10FC0422C6F9870096A34E /* AliyunSVideoApi.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AliyunSVideoApi.h; sourceTree = "<group>"; };
 		BD10FC0622C748D90096A34E /* AlivcAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AlivcAlertView.m; sourceTree = "<group>"; };
 		BD10FC0722C748DA0096A34E /* AlivcAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AlivcAlertView.h; sourceTree = "<group>"; };
+		BD11D64122E02520008FFA58 /* MusicPlayManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicPlayManager.swift; sourceTree = "<group>"; };
 		BD12203522AF807C0051C7C2 /* MessagePlanetActivityController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagePlanetActivityController.swift; sourceTree = "<group>"; };
 		BD12203922AF80AD0051C7C2 /* MessageActivityTableCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageActivityTableCell.swift; sourceTree = "<group>"; };
 		BD12203E22AF8E190051C7C2 /* MessagePlanetNotiController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessagePlanetNotiController.swift; sourceTree = "<group>"; };
@@ -2319,6 +2321,7 @@
 				A72A730522321F1D00B21995 /* PayManager */,
 				A72A726122321DBD00B21995 /* MapManager */,
 				A72A726422321DBD00B21995 /* UMManager */,
+				BD11D64022E024DC008FFA58 /* MusicPlayManager */,
 			);
 			path = Manager;
 			sourceTree = "<group>";
@@ -4670,6 +4673,14 @@
 			path = HGImagePicker;
 			sourceTree = "<group>";
 		};
+		BD11D64022E024DC008FFA58 /* MusicPlayManager */ = {
+			isa = PBXGroup;
+			children = (
+				BD11D64122E02520008FFA58 /* MusicPlayManager.swift */,
+			);
+			path = MusicPlayManager;
+			sourceTree = "<group>";
+		};
 		BD12203222AF803A0051C7C2 /* MessagePlanetActivity */ = {
 			isa = PBXGroup;
 			children = (
@@ -5945,6 +5956,7 @@
 				A77F2CC722320627001BD3F6 /* WRNavigationBar.swift in Sources */,
 				A7274C5E228EE636000E3A07 /* LBXPermissions.swift in Sources */,
 				A7D46092227619CD00A5A54E /* BaiduToCityFactory.swift in Sources */,
+				BD11D64222E02520008FFA58 /* MusicPlayManager.swift in Sources */,
 				A71AA51C227219EF008FF1A5 /* EditExpressAddressView.swift in Sources */,
 				BDAA40FB228E9CC300CF841D /* OrderApplyRefundNoteInfoCell.swift in Sources */,
 				A7DF50D622A4E8B400998908 /* OrderDetailProductTableViewCell.swift in Sources */,

+ 105 - 0
RainbowPlanet/RainbowPlanet/Manager/MusicPlayManager/MusicPlayManager.swift

@@ -0,0 +1,105 @@
+//
+//  MusicPlayManager.swift
+//  RainbowPlanet
+//
+//  Created by Christopher on 2019/7/18.
+//  Copyright © 2019 RainbowPlanet. All rights reserved.
+//
+
+import UIKit
+
+class MusicPlayManager: NSObject {
+    
+    private static let _sharedInstance = MusicPlayManager()
+    
+    private override init() {} // 私有化init方法
+    
+    class func shared() -> MusicPlayManager {
+        return _sharedInstance
+    }
+    
+    // 记录当前正在播放的音乐Id
+    var curPlayingId: Int = -1
+    
+    //播放器相关
+    var playerItem: AVPlayerItem!
+    var musicPlayer: AVPlayer!
+    
+    // 音频url
+    var audioUrl: String = "" {
+        didSet{
+            self.setupPlayerItem()
+        }
+    }
+    
+    func initPlay() {
+        //初始化播放器
+        musicPlayer = AVPlayer()
+        //监听音频播放结束
+        NotificationCenter.default.addObserver(self, selector: #selector(playItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: MusicPlayManager.shared().playerItem)
+    }
+    
+    //设置资源
+    private func setupPlayerItem() {
+        guard let url = URL(string: audioUrl) else {
+            return
+        }
+        self.playerItem = AVPlayerItem(url: url)
+        self.musicPlayer.replaceCurrentItem(with: playerItem)
+    }
+    
+    //获取音频时长
+    func getDuration() -> Float64 {
+        if MusicPlayManager.shared().playerItem == nil {
+            return 0.0
+        }
+        let duration : CMTime = playerItem!.asset.duration
+        let seconds : Float64 = CMTimeGetSeconds(duration)
+        return seconds
+    }
+    func getCurrentTime() -> Float64 {
+        if MusicPlayManager.shared().playerItem == nil {
+            return 0.0
+        }
+        let duration : CMTime = playerItem!.currentTime()
+        let seconds : Float64 = CMTimeGetSeconds(duration)
+        return seconds
+    }
+    
+    //播放结束
+    var audioPlayEndBlock:(()->())?
+    @objc func playItemDidReachEnd(notifacation:NSNotification) {
+        musicPlayer?.seek(to: CMTime.zero)
+        if let block = audioPlayEndBlock {
+            block()
+        }
+    }
+    
+    //播放
+    func playAudio() {
+        if musicPlayer != nil {
+            musicPlayer?.play()
+        }
+    }
+    
+    //暂停
+    var audioStopBlock:(()->())?
+    func stopAudio() {
+        if musicPlayer != nil {
+            musicPlayer?.pause()
+            if let block = audioStopBlock {
+                block()
+            }
+        }
+    }
+    
+    //销毁
+    func destroyPlayer() {
+        if MusicPlayManager.shared().playerItem != nil {
+            MusicPlayManager.shared().musicPlayer?.pause()
+            MusicPlayManager.shared().playerItem?.cancelPendingSeeks()
+            MusicPlayManager.shared().playerItem?.asset.cancelLoading()
+        }
+    }
+    
+}

+ 4 - 0
RainbowPlanet/RainbowPlanet/Model/CommunityModel/CommunityRecommendMusicListModel.swift

@@ -42,6 +42,10 @@ class CommunityMusicItemModel : NSObject, Mappable{
     var singer : String?
     var url : String?
     
+    // 本地添加,用于控制状态
+    var isPlaying : Bool? = false
+    var isChoosing : Bool? = false
+    
     
     class func newInstance(map: Map) -> Mappable?{
         return CommunityMusicItemModel()

+ 12 - 7
RainbowPlanet/RainbowPlanet/Modules/PublishModule/AliyunVideo/AlivcShortVideo/ShortVideoFile/VideoRecord/Controller/AliyunMagicCameraViewController.m

@@ -157,6 +157,9 @@
 
 @property(nonatomic, strong) AliyunEffectFilterView *filterView;
 
+// 配乐のView
+@property (nonatomic, strong) PublishMusicChooseView *musicView;
+
 @end
 
 @implementation AliyunMagicCameraViewController
@@ -741,13 +744,15 @@
 
 - (void)musicButtonClicked
 {
-    AliyunMusicPickViewController *vc =[[AliyunMusicPickViewController alloc] init];
-    vc.delegate = self;
-    vc.duration = _clipManager.maxDuration;
-    vc.selectedMusic = self.music;
-    vc.selectedTab = self.tab;
-    self.needStopPreview =NO;
-    [self.navigationController pushViewController:vc animated:YES];
+//    AliyunMusicPickViewController *vc =[[AliyunMusicPickViewController alloc] init];
+//    vc.delegate = self;
+//    vc.duration = _clipManager.maxDuration;
+//    vc.selectedMusic = self.music;
+//    vc.selectedTab = self.tab;
+//    self.needStopPreview =NO;
+//    [self.navigationController pushViewController:vc animated:YES];
+    
+    self.musicView = [PublishMusicChooseView publishMusicChooseViewWithAttachedView:self.view];
 }
 
 - (void)filterButtonClicked {

+ 32 - 16
RainbowPlanet/RainbowPlanet/Modules/PublishModule/PublishMusicAbout/PublishMusicChooseView.swift

@@ -32,7 +32,6 @@ class PublishMusicChooseView: FWPopupView {
         fatalError("init(coder:) has not been implemented")
     }
     
-    var JXheightForHeaderInSection: Int = 44
     var categoryTitleArr = Array<String>()
     
     var categoryListMdlArr : Array<MusicCategoryItemModel>? {
@@ -126,15 +125,14 @@ class PublishMusicChooseView: FWPopupView {
     func setupSegmentedView() {
         addSubview(segmentedView)
         addSubview(listContainerView)
-        reloadData()
+//        reloadData()
     }
     
     /// 刷新页面
     func reloadData() {
         segmentedDataSource.titles = categoryTitleArr
         segmentedView.indicators = [indicator]
-
-        JXheightForHeaderInSection = 44
+        
         
         //        self.segmentedDataSource.reloadData(selectedIndex: shoppingMallListVCType)
         //        self.segmentedView.defaultSelectedIndex = shoppingMallListVCType
@@ -197,14 +195,18 @@ class PublishMusicChooseView: FWPopupView {
     //MARK: -
     //1.初始化JXSegmentedView
     lazy var segmentedView: JXSegmentedView = {
-        let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 48, width: kScreenWidth, height: CGFloat(JXheightForHeaderInSection)))
+        let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 48, width: kScreenWidth, height: 44))
         segmentedView.delegate = self
         segmentedView.dataSource = segmentedDataSource
         segmentedView.indicators = [indicator]
         segmentedView.contentScrollView = listContainerView.scrollView
-        segmentedView.selectItemAt(index: 0)
         segmentedView.defaultSelectedIndex = 0
-        segmentedView.backgroundColor = UIColor.yellow
+        
+        let lineWidth: CGFloat = 0.5
+        let lineLayer = CALayer()
+        lineLayer.backgroundColor = k999999Color.cgColor
+        lineLayer.frame = CGRect(x: 0, y: segmentedView.bounds.height - lineWidth, width: segmentedView.bounds.width, height: lineWidth)
+        segmentedView.layer.addSublayer(lineLayer)
         return segmentedView
     }()
     
@@ -214,8 +216,9 @@ class PublishMusicChooseView: FWPopupView {
         segmentedDataSource.titles = categoryTitleArr
         segmentedDataSource.isTitleColorGradientEnabled = true
         segmentedDataSource.isItemSpacingAverageEnabled = true
-        segmentedDataSource.titleNormalColor = kffffffColor
-        segmentedDataSource.titleSelectedColor = kbbbbbbColor
+        segmentedDataSource.isTitleZoomEnabled = true
+        segmentedDataSource.titleNormalColor = kbbbbbbColor
+        segmentedDataSource.titleSelectedColor = kffffffColor
         segmentedDataSource.titleNormalFont = kRegularFont14!
         segmentedDataSource.titleSelectedFont = kMediumFont14
         
@@ -227,9 +230,11 @@ class PublishMusicChooseView: FWPopupView {
     //3.初始化指示器indicator
     lazy var indicator: JXSegmentedIndicatorLineView = {
         let indicator = JXSegmentedIndicatorLineView()
-        indicator.indicatorColor = kFFA42FColor
-        indicator.indicatorHeight = 4
-        indicator.indicatorWidth = 34
+        indicator.indicatorColor = kffffffColor
+        indicator.indicatorHeight = 3
+        indicator.indicatorWidth = 20
+        indicator.cornerRadius = 1.5
+        indicator.masksToBounds = true
         return indicator
     }()
     
@@ -238,8 +243,7 @@ class PublishMusicChooseView: FWPopupView {
         let listContainerView = JXSegmentedListContainerView(dataSource: self)
         listContainerView.didAppearPercent = 0.01
         listContainerView.defaultSelectedIndex = 0
-        listContainerView.scrollView.isScrollEnabled = true
-        listContainerView.frame = CGRect(x: 0, y: 48+44, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 103 - 114)        
+        listContainerView.frame = CGRect(x: 0, y: 48+44, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 103 - 114)
         return listContainerView
     }()
     
@@ -252,12 +256,15 @@ class PublishMusicChooseView: FWPopupView {
         vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
         vProperty.popupAnimationType = .scale
         vProperty.maskViewColor = UIColor(white: 0, alpha: 0.2)
-        vProperty.touchWildToHide = "1"
+        // 用户点击外部遮罩层页面是否可以消失
+        vProperty.touchWildToHide = "0"
         view.vProperty = vProperty
         view.show()
         
         view.cancelButton.rx.tap.subscribe(onNext: { (data) in            
             view.hide()
+            MusicPlayManager.shared().destroyPlayer()
+            MusicPlayManager.shared().curPlayingId = -1
             
         }).disposed(by: view.disposeBag)
         
@@ -315,7 +322,10 @@ extension PublishMusicChooseView : JXSegmentedViewDelegate {
 extension PublishMusicChooseView :JXSegmentedListContainerViewDataSource {
     
     func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
-        return categoryTitleArr.count
+        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
+            return titleDataSource.dataSource.count
+        }
+        return 0
     }
     func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
         let listVc = PublishMusicListController()
@@ -324,6 +334,12 @@ extension PublishMusicChooseView :JXSegmentedListContainerViewDataSource {
         } else {
             listVc.categoryItemMdl = categoryListMdlArr![index-1]
         }
+        listVc.selMusicClosure = {
+            [weak self] (musicUrl) in
+            MusicPlayManager.shared().initPlay()
+            MusicPlayManager.shared().audioUrl = musicUrl
+            MusicPlayManager.shared().playAudio()
+        }
         return listVc
     }
 }

+ 20 - 6
RainbowPlanet/RainbowPlanet/Modules/PublishModule/PublishMusicAbout/PublishMusicListCell.swift

@@ -15,13 +15,27 @@ class PublishMusicListCell: UITableViewCell {
     var musicItemMdl: CommunityMusicItemModel? {
         didSet {
             titleLabel.text = self.musicItemMdl?.name
+            
+            if self.musicItemMdl?.isPlaying == true {
+                iconImageView.image = kImage(name: "music_btn_pause")
+                useButton.isHidden = false
+            } else {
+                iconImageView.image = kImage(name: "music_btn_play")
+                useButton.isHidden = true
+            }
+            
+            if self.musicItemMdl?.isChoosing == true {
+                curPlayImageView.isHidden = false
+            } else {
+                curPlayImageView.isHidden = true
+            }
         }
     }
     
     let disposeBag = DisposeBag()
     
-    typealias BtnClickClosure = () -> Void
-    var btnClickClosure : BtnClickClosure?
+    typealias UseClickClosure = () -> Void
+    var useClickClosure : UseClickClosure?
     
     class func cellWith(tableView:UITableView,indexPath:IndexPath) -> PublishMusicListCell {
         let ID = "PublishMusicListCell"
@@ -94,8 +108,7 @@ class PublishMusicListCell: UITableViewCell {
     }()
     
     private lazy var titleLabel: UILabel = {
-        let titleLabel = UILabel()
-        titleLabel.text = "最快乐的一天"
+        let titleLabel = UILabel()        
         titleLabel.textColor = kffffffColor
         titleLabel.font = kMediumFont14
         titleLabel.textAlignment = .left
@@ -106,7 +119,6 @@ class PublishMusicListCell: UITableViewCell {
         let curPlayImageView = UIImageView()
         curPlayImageView.image = kImage(name: "music_ico_selected")
         return curPlayImageView
-        //        music_btn_pause
     }()
     
     private lazy var useButton: UIButton = {
@@ -118,7 +130,9 @@ class PublishMusicListCell: UITableViewCell {
         useButton.layer.cornerRadius = kScaleValue(value: 13)
         useButton.layer.masksToBounds = true
         useButton.rx.tap.subscribe(onNext: { [weak self] in
-            print("----点击了-使用")
+            if let useClickClosure = self?.useClickClosure {
+                useClickClosure()
+            }
         }).disposed(by: disposeBag)
         return useButton
     }()

+ 49 - 3
RainbowPlanet/RainbowPlanet/Modules/PublishModule/PublishMusicAbout/PublishMusicListController.swift

@@ -17,6 +17,9 @@ class PublishMusicListController: BaseViewController {
         listViewDidScrollCallback = nil
     }
     
+    typealias SelMusicClosure = (_ musicUrl: String) -> Void
+    var selMusicClosure : SelMusicClosure?
+    
     // 推荐下の音乐
     var isReccomendList: Bool? {
         didSet {
@@ -59,7 +62,6 @@ class PublishMusicListController: BaseViewController {
     lazy var tableView: UITableView = {
         let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
         tableView.separatorStyle = .none
-//        tableView.backgroundColor = kRGBColor(r: 255, g: 192, b: 203)
         tableView.backgroundColor = UIColor.clear
         tableView.dataSource = self
         tableView.delegate = self
@@ -71,21 +73,38 @@ class PublishMusicListController: BaseViewController {
     
 }
 
+// MARK: - tableView dataSource && delegate
 extension PublishMusicListController : UITableViewDelegate, UITableViewDataSource {
     func numberOfSections(in tableView: UITableView) -> Int {
         return 1
     }
     
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-        return musicItemMdlArr.isEmpty ?? true ? 0 : musicItemMdlArr.count
+        return musicItemMdlArr.isEmpty ? 0 : musicItemMdlArr.count
     }
     
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = PublishMusicListCell.cellWith(tableView: tableView, indexPath: indexPath)
         cell.musicItemMdl = musicItemMdlArr[indexPath.row]
+        cell.useClickClosure = {
+            [weak self] in
+            let musicUrlStr = self?.musicItemMdlArr[indexPath.row].url
+            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DownloadMusicAndCombineNoti"), object: musicUrlStr)
+        }
         return cell
     }
     
+    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        let musicUrl = musicItemMdlArr[indexPath.row].url!
+        if let selMusicClosure = self.selMusicClosure {
+            selMusicClosure(musicUrl)
+        }
+        
+        let musicId = musicItemMdlArr[indexPath.row].id!
+        MusicPlayManager.shared().curPlayingId = musicId
+        resetPlayingStatusWithout(musicId)
+    }
+    
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
         return 50
     }
@@ -111,12 +130,14 @@ extension PublishMusicListController : UITableViewDelegate, UITableViewDataSourc
     
 }
 
+// MARK: - JXContainerListDelegate
 extension PublishMusicListController : JXSegmentedListContainerViewListDelegate {
     func listView() -> UIView {
         return view
     }
     func listDidAppear() {
-        
+        let curPlayingId = MusicPlayManager.shared().curPlayingId
+        self.resetPlayingStatusWithout(curPlayingId)
     }
     func listDidDisappear() {
         
@@ -166,4 +187,29 @@ extension PublishMusicListController {
             }
         }
     }
+    
+    // 重置播放状态
+    func resetPlayingStatusWithout(_ playingId: Int) -> Void {
+        for musicMdl in musicItemMdlArr {
+            if playingId == musicMdl.id {
+                musicMdl.isPlaying = true
+            } else {
+                musicMdl.isPlaying = false
+            }
+        }
+        tableView.reloadData()
+    }
+    
+    // 重置选中状态
+    func resetChoosingStatus(_ playingId: Int) -> Void {
+        for musicMdl in musicItemMdlArr {
+            if playingId == musicMdl.id {
+                musicMdl.isPlaying = true
+            } else {
+                musicMdl.isPlaying = false
+            }
+        }
+        tableView.reloadData()
+    }
+    
 }