| | |
| | | let tap = UITapGestureRecognizer(target: self, action: #selector(showDetailAction)) |
| | | coverImage.isUserInteractionEnabled = true |
| | | coverImage.addGestureRecognizer(tap) |
| | | |
| | | |
| | | NotificationCenter.default.rx.notification(UIApplication.didBecomeActiveNotification).subscribe(onNext: {noti in |
| | | if self.audioPlayer.bgmPlayer?.timeControlStatus == .playing{ |
| | | PayMusicVC.updateStatus(.playing) |
| | | }else{ |
| | | PayMusicVC.updateStatus(.pause) |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | } |
| | | |
| | | override func setUI() { |
| | |
| | | } |
| | | } |
| | | |
| | | static func show(model:MeditationModel){ |
| | | static func show(model:MeditationModel,isHidden:Bool = true){ |
| | | if let tabBarVC = JQ_currentViewController().navigationController?.tabBarController as? BaseTabBarVC{ |
| | | if !tabBarVC.children.contains(where: {$0 is PayMusicVC}){ |
| | | let vc = PayMusicVC() |
| | | vc.view.isHidden = true |
| | | vc.view.isHidden = isHidden |
| | | vc.meditationModel = model |
| | | let tabBarHeight = JQ_currentViewController().navigationController?.tabBarController?.tabBar.height ?? 0 |
| | | JQ_currentViewController().navigationController?.tabBarController?.addChild(vc) |
| | |
| | | make.height.equalTo(46.5) |
| | | make.bottom.equalToSuperview().offset(-(tabBarHeight)) |
| | | } |
| | | vc.view.layoutIfNeeded() |
| | | vc.startRunloopAni() |
| | | }else{ |
| | | PayMusicVC.updateStatus(.playing) |
| | | PayMusicVC.updateStatus(.playing, musicName: model.meditationTitle) |
| | | } |
| | | } |
| | | } |
| | | |
| | | static func updateStatus(_ state:PayMuiscState){ |
| | | static func updateStatus(_ state:PayMuiscState,musicName:String? = nil){ |
| | | if let tabBarVC = JQ_currentViewController().navigationController?.tabBarController as? BaseTabBarVC{ |
| | | if let vc = tabBarVC.children.filter({$0 is PayMusicVC}).first as? PayMusicVC{ |
| | | vc.btn_handle.isSelected = state == .pause |
| | | if let musicName{ |
| | | vc.label_name.text = musicName |
| | | } |
| | | switch state { |
| | | case .playing: |
| | | MPNowPlayingInfoCenter.default().playbackState = .playing |
| | |
| | | |
| | | @objc func showDetailAction(){ |
| | | if let id = meditationModel?.id{ |
| | | let vc = HomeItemDetailVC(id: id) |
| | | vc.hidesBottomBarWhenPushed = true |
| | | JQ_currentNavigationController().pushViewController(vc) |
| | | } |
| | | Services.getMeditationDetail(id: id).subscribe(onNext: {[weak self]data in |
| | | guard let weakSelf = self else { return } |
| | | if let m = data.data{ |
| | | let isVip = m.isVip == .yes |
| | | if m.chargeType == .free || (isVip && m.chargeType == .vipFree) || (m.chargeType == .payment && m.isBuy == .yes){ |
| | | let vc = HomeItemDetailVC(model: m) |
| | | vc.hidesBottomBarWhenPushed = true |
| | | JQ_currentNavigationController().jq_push(vc: vc) |
| | | return |
| | | } |
| | | |
| | | if m.chargeType == .payment && m.isBuy == .no{ |
| | | guard sceneDelegate!.checkisLoginState() else{return} |
| | | let vc = PaymentOrderVC(museItemModel: m,type: .muse,showType:.vertical) |
| | | vc.hidesBottomBarWhenPushed = true |
| | | JQ_currentNavigationController().jq_push(vc: vc) |
| | | return |
| | | } |
| | | |
| | | if m.chargeType == .vipFree{ |
| | | let vc = VIPCenterVC() |
| | | vc.hidesBottomBarWhenPushed = true |
| | | JQ_currentNavigationController().jq_push(vc: vc) |
| | | return |
| | | } |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | } |
| | | } |
| | | |
| | | private func startRunloopAni(){ |
| | |
| | | self.stopRunloopAni() |
| | | PayMusicVC.updateStatus(.pause) |
| | | }else{ |
| | | |
| | | if self.audioPlayer.scenePlayer?.timeControlStatus == .playing{ |
| | | self.audioPlayer.scenePlayer?.pause() |
| | | } |
| | | |
| | | self.audioPlayer.bgmPlayer?.play() |
| | | self.audioPlayer.masterPlayer?.play() |
| | | self.audioPlayer.masterPlayer?.volume = Float(UserDefaultSettingViewModel.getSetting()?.masterVolume ?? 0.5) |
| | |
| | | } |
| | | |
| | | @objc func closeAction(_ btn:UIButton){ |
| | | CommonAlertView.show(title: "提示", content: "是否关闭当前播放音频?") { state in |
| | | CommonAlertView.show(title: "提示", content: "是否关闭当前播放音频?") {[weak self] state in |
| | | guard let weakSelf = self else { return } |
| | | if state{ |
| | | self.audioPlayer.clean() |
| | | self.view.removeFromSuperview() |
| | | self.removeFromParent() |
| | | self?.audioPlayer.cleanMuse() |
| | | self?.view.removeFromSuperview() |
| | | self?.removeFromParent() |
| | | |
| | | if let id = self?.audioPlayer.meditationModel?.id{ |
| | | Services.watchMuse(id: id, timeLook: self?.audioPlayer.lisenMuseTime ?? 0).subscribe(onNext: { _ in |
| | | NotificationCenter.default.post(name: TreeTaskUpdate_Noti, object: nil) |
| | | }).disposed(by: weakSelf.disposeBag) |
| | | } |
| | | |
| | | if UserDefaultSettingViewModel.getSetting()?.bgm != nil{ |
| | | AudioPlayer.getSharedInstance().playSceneAt(UserDefaultSettingViewModel.getSetting()!.bgm!.audioFile) |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | private var timer:Timer? |
| | | private(set) var times = BehaviorRelay<Int?>(value: nil) //倒计时定时器 |
| | | private static var _sharedInstance: AudioPlayer? |
| | | private(set) var meditationModel:MeditationModel? |
| | | private(set) var meditationModel:MeditationModel? //疗愈音频 |
| | | var lisenMuseTime:Int = 0 //听疗愈时间记录 |
| | | |
| | | weak var delegate:PayMusicDelegate? |
| | | private var disposeBag = DisposeBag() |
| | | |
| | |
| | | } |
| | | |
| | | private init() { |
| | | |
| | | NotificationCenter.default.addObserver(forName: ChangeMasterVoice_Noti, object: nil, queue: .main) { noti in |
| | | if let value = noti.object as? Float{ |
| | | print("--->进入") |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | NotificationCenter.default.rx.notification(.AVPlayerItemDidPlayToEndTime).subscribe(onNext: {[weak self]data in |
| | | |
| | | guard let weakSelf = self else { return } |
| | | |
| | | let notifiUrl = ((data.object as? AVPlayerItem)?.asset as? AVURLAsset) |
| | | let sceneItem = (self?.scenePlayer?.currentItem?.asset as? AVURLAsset) |
| | | let bgmItem = (self?.bgmPlayer?.currentItem?.asset as? AVURLAsset) |
| | | let masterItem = (self?.masterPlayer?.currentItem?.asset as? AVURLAsset) |
| | | |
| | | |
| | | if masterItem?.url == notifiUrl?.url{ |
| | | weakSelf.masterPlayer = nil |
| | | } |
| | | |
| | | |
| | | if bgmItem?.url == notifiUrl?.url{ |
| | | // self?.bgmPlayer?.seek(to: CMTime.zero) |
| | | // self?.bgmPlayer?.play() |
| | | |
| | | if weakSelf.playIndex < weakSelf.urls.count - 1{ |
| | | weakSelf.next() |
| | | weakSelf.delegate?.playState(.next) |
| | |
| | | weakSelf.playIndex = 0 |
| | | weakSelf.bgmPlayer?.replaceCurrentItem(with: AVPlayerItem(url: weakSelf.urls[0])) |
| | | weakSelf.bgmPlayer?.play() |
| | | |
| | | // weakSelf.delegate?.playState(.end) |
| | | // PayMusicVC.hidden() |
| | | // MPNowPlayingInfoCenter.default().nowPlayingInfo = nil |
| | | } |
| | | |
| | | if let id = weakSelf.meditationModel?.id{ |
| | | Services.watchMuse(id: id, timeLook: weakSelf.lisenMuseTime).subscribe(onNext: { _ in |
| | | weakSelf.lisenMuseTime = 0 |
| | | NotificationCenter.default.post(name: TreeTaskUpdate_Noti, object: nil) |
| | | NotificationCenter.default.post(name: CleanDetailTime_Noti, object: nil) |
| | | }).disposed(by: weakSelf.disposeBag) |
| | | } |
| | | } |
| | | |
| | | if sceneItem?.url == notifiUrl?.url{ |
| | |
| | | } |
| | | |
| | | }).disposed(by: disposeBag) |
| | | |
| | | |
| | | // //播放完成 |
| | | // NotificationCenter.default.addObserver(self, selector: #selector(playBGMbackEnd), name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) |
| | | |
| | | |
| | | } // 私有化init方法 |
| | | |
| | | //销毁单例对象 |
| | |
| | | self.bgmPlayer?.pause() |
| | | self.masterPlayer?.pause() |
| | | self.meditationModel = nil |
| | | self.masterPlayer = nil |
| | | self.bgmPlayer = nil |
| | | self.timer = nil |
| | | AudioPlayer.destroy() |
| | | } |
| | | |
| | | func cleanMuse(){ |
| | | self.bgmPlayer?.pause() |
| | | self.masterPlayer?.pause() |
| | | self.meditationModel = nil |
| | | self.masterPlayer = nil |
| | | self.timer = nil |
| | | self.lisenMuseTime = 0 |
| | | } |
| | | |
| | | func playBGMAt(firstPlayIndex:Int,model:MeditationModel,delegate:PayMusicDelegate?,coverImage:UIImage? = nil){ |
| | | |
| | |
| | | } |
| | | } |
| | | self.bgmPlayer = AVPlayer(url: self.urls[firstPlayIndex]) |
| | | self.bgmPlayer?.play() |
| | | |
| | | if masterUrl != nil{ |
| | | self.masterPlayer = AVPlayer(url: masterUrl!) |
| | | self.masterPlayer?.volume = Float(UserDefaultSettingViewModel.getSetting()?.masterVolume ?? 0.5) |
| | | self.masterPlayer?.play() |
| | | } |
| | | } |
| | | } |
| | | |
| | | self.bgmPlayer!.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 1), queue: DispatchQueue.main) { [weak self](time) in |
| | | self.bgmPlayer?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 1), queue: DispatchQueue.main) { [weak self](time) in |
| | | guard let weakSelf = self else { return } |
| | | weakSelf.delegate?.playState(.playing) |
| | | |
| | | guard weakSelf.bgmPlayer != nil else {return} |
| | | weakSelf.lisenMuseTime += 1 |
| | | //当前正在播放的时间 |
| | | let loadTime = CMTimeGetSeconds(time) |
| | | //视频总时间 |
| | | let totalTime = CMTimeGetSeconds((weakSelf.bgmPlayer?.currentItem?.duration)!) |
| | | |
| | | |
| | | // if loadTime >= totalTime{ |
| | | // if weakSelf.playIndex < urls.count - 1{ |
| | | // weakSelf.next() |
| | | // weakSelf.delegate?.playState(.next) |
| | | // }else{ |
| | | // weakSelf.bgmPlayer?.pause() |
| | | // weakSelf.playIndex = 0 |
| | | // weakSelf.bgmPlayer?.replaceCurrentItem(with: AVPlayerItem(url: urls[0])) |
| | | // weakSelf.bgmPlayer?.play() |
| | | |
| | | // weakSelf.delegate?.playState(.end) |
| | | // PayMusicVC.hidden() |
| | | // MPNowPlayingInfoCenter.default().nowPlayingInfo = nil |
| | | // } |
| | | // } |
| | | |
| | | weakSelf.delegate?.playListen?(currentInterval: loadTime, totalInterval: totalTime) |
| | | |
| | | if let m = weakSelf.meditationModel{ |
| | | var dic = [String:Any]() |
| | | dic[MPMediaItemPropertyTitle] = m |
| | | dic[MPMediaItemPropertyArtist] = "心泉·疗愈" |
| | | dic[MPMediaItemPropertyArtist] = "泉疗愈" |
| | | dic[MPMediaItemPropertyDiscNumber] = 1 |
| | | dic[MPNowPlayingInfoPropertyElapsedPlaybackTime] = loadTime |
| | | dic[MPNowPlayingInfoPropertyPlaybackRate] = 1 |
| | |
| | | } |
| | | } |
| | | |
| | | self.bgmPlayer?.currentItem?.rx.observe(AVPlayerItem.Status.self,"status").subscribe(onNext: { _ in |
| | | |
| | | if self.bgmPlayer?.status == .readyToPlay && self.masterPlayer?.status == .readyToPlay{ |
| | | self.bgmPlayer?.play() |
| | | self.masterPlayer?.play() |
| | | } |
| | | |
| | | }).disposed(by: disposeBag) |
| | | |
| | | self.masterPlayer?.currentItem?.rx.observe(AVPlayerItem.Status.self,"status").subscribe(onNext: { _ in |
| | | print("---MasterStatus: \(self.masterPlayer?.status.rawValue ?? 0)") |
| | | if self.bgmPlayer?.status == .readyToPlay && self.masterPlayer?.status == .readyToPlay{ |
| | | self.bgmPlayer?.play() |
| | | self.masterPlayer?.play() |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | |
| | | setLockScreen() |
| | | |
| | | do { |
| | |
| | | print(error) |
| | | } |
| | | } |
| | | |
| | | |
| | | func setBGMRate(value:CGFloat){ |
| | | if let v = bgmPlayer?.currentItem?.duration.timescale,v > 0{ |
| | |
| | | } |
| | | |
| | | func playScene(){ |
| | | scenePlayer?.play() |
| | | if UserDefaultSettingViewModel.getSetting()?.bgm != nil{ |
| | | playSceneAt(UserDefaultSettingViewModel.getSetting()?.bgm?.audioFile ?? "") |
| | | } |
| | | } |
| | | |
| | | func next(){ |
| | |
| | | bgmPlayer?.replaceCurrentItem(with: AVPlayerItem(url: urls[index])) |
| | | bgmPlayer?.play() |
| | | } |
| | | |
| | | // @objc private func playBGMbackEnd(){ |
| | | // self.scenePlayer?.seek(to: CMTimeMake(value: 0, timescale: 1)) |
| | | // self.scenePlayer?.play() |
| | | // } |
| | | |
| | | func previous(){ |
| | | playIndex -= 1 |
| | |
| | | } |
| | | |
| | | // 播放 |
| | | center.playCommand.addTarget {[unowned self] event in |
| | | self.bgmPlayer?.play() |
| | | center.playCommand.addTarget {[weak self] event in |
| | | self?.bgmPlayer?.play() |
| | | self?.masterPlayer?.play() |
| | | PayMusicVC.updateStatus(.playing) |
| | | return .success |
| | | } |
| | | |
| | | // 暂停 |
| | | center.pauseCommand.addTarget {[unowned self] event in |
| | | self.bgmPlayer?.pause() |
| | | center.pauseCommand.addTarget {[weak self] event in |
| | | self?.bgmPlayer?.pause() |
| | | self?.masterPlayer?.pause() |
| | | PayMusicVC.updateStatus(.pause) |
| | | return .success |
| | | } |