| | |
| | | import AVFoundation |
| | | import MediaPlayer |
| | | import RxRelay |
| | | import RxSwift |
| | | |
| | | |
| | | let ChangeMasterVoice_Noti = Notification.Name.init(rawValue: "ChangeMasterVoice_Noti") |
| | |
| | | private static var _sharedInstance: AudioPlayer? |
| | | private(set) var meditationModel:MeditationModel? |
| | | weak var delegate:PayMusicDelegate? |
| | | private var disposeBag = DisposeBag() |
| | | |
| | | class func getSharedInstance() -> AudioPlayer { |
| | | guard let instance = _sharedInstance else { |
| | |
| | | self.masterPlayer?.volume = value |
| | | } |
| | | } |
| | | |
| | | |
| | | 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) |
| | | |
| | | 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) |
| | | }else{ |
| | | weakSelf.bgmPlayer?.pause() |
| | | 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 sceneItem?.url == notifiUrl?.url{ |
| | | self?.scenePlayer?.seek(to: CMTime.zero) |
| | | self?.scenePlayer?.play() |
| | | } |
| | | |
| | | }).disposed(by: disposeBag) |
| | | |
| | | |
| | | // //播放完成 |
| | | // NotificationCenter.default.addObserver(self, selector: #selector(playBGMbackEnd), name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) |
| | | |
| | | |
| | | } // 私有化init方法 |
| | | |
| | | //销毁单例对象 |
| | |
| | | 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() |
| | | // 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) |
| | | |
| | |
| | | |
| | | /// 播放场景音乐 |
| | | func playSceneAt(_ url:String){ |
| | | guard let URL = URL(string: url) else { return } |
| | | guard let URL = URL(string: url.jq_urlEncoded()) else { return } |
| | | scenePlayer?.pause() |
| | | |
| | | if scenePlayer == nil{ |
| | |
| | | self.scenePlayer?.play() |
| | | self.scenePlayer?.volume = Float(UserDefaultSettingViewModel.getSetting()?.volume ?? 0.5) |
| | | } |
| | | |
| | | //播放完成 |
| | | NotificationCenter.default.addObserver(self, selector: #selector(playBGMbackEnd), name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) |
| | | } |
| | | |
| | | func dellocScene(){ |
| | |
| | | bgmPlayer?.play() |
| | | } |
| | | |
| | | @objc private func playBGMbackEnd(){ |
| | | self.scenePlayer?.seek(to: CMTimeMake(value: 0, timescale: 1)) |
| | | self.scenePlayer?.play() |
| | | } |
| | | // @objc private func playBGMbackEnd(){ |
| | | // self.scenePlayer?.seek(to: CMTimeMake(value: 0, timescale: 1)) |
| | | // self.scenePlayer?.play() |
| | | // } |
| | | |
| | | func previous(){ |
| | | playIndex -= 1 |