|
@@ -21,7 +21,9 @@ class BFMusicSearchController: BFBaseViewController {
|
|
|
|
|
|
var loadedTimeRangesObserver : NSKeyValueObservation?
|
|
|
|
|
|
- var chosedCell : BFMusicInfoSearchCell?
|
|
|
+ var chosedIndexPath : IndexPath?
|
|
|
+
|
|
|
+ var chosedCellStatu : BFMuicInfoCellState = .pause
|
|
|
|
|
|
var searchResults = [PQVoiceModel]()
|
|
|
|
|
@@ -95,10 +97,11 @@ class BFMusicSearchController: BFBaseViewController {
|
|
|
guard let wself = self else { return }
|
|
|
|
|
|
guard let ranges = change.newValue as? [CMTimeRange] else { return }
|
|
|
- if let cell = wself.chosedCell, let totalDur = player.currentItem?.duration, totalDur.isValid, CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
|
|
|
+ if let indx = wself.chosedIndexPath, let cell = wself.musicTb.cellForRow(at: indx) as? BFMuicInfoCell, let totalDur = player.currentItem?.duration, totalDur.isValid, CMTimeCompare(ranges.first?.duration ?? .zero, totalDur) >= 0{
|
|
|
if (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" == cell.data?.musicPath ?? "a" {
|
|
|
if cell.status == .loading{
|
|
|
cell.status = .playing
|
|
|
+ wself.chosedCellStatu = .playing
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -108,6 +111,13 @@ class BFMusicSearchController: BFBaseViewController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ func choseCell() -> BFMuicInfoCell? {
|
|
|
+ if let indpx = chosedIndexPath, let cell = musicTb.cellForRow(at: indpx) as? BFMuicInfoCell {
|
|
|
+ return cell
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
@objc func closeAction(){
|
|
|
self.dismiss(animated: true, completion: nil)
|
|
|
}
|
|
@@ -143,6 +153,13 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if cell.data?.isSelected ?? false {
|
|
|
+ chosedIndexPath = indexPath
|
|
|
+ cell.status = chosedCellStatu
|
|
|
+ }else{
|
|
|
+ cell.status = .normal
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
cell?.selectionStyle = .none
|
|
@@ -157,16 +174,35 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
|
if let cell = tableView.cellForRow(at: indexPath) as? BFMusicInfoSearchCell {
|
|
|
-
|
|
|
+ if chosedIndexPath == indexPath {
|
|
|
+ if cell.status != .pause{
|
|
|
+ cell.status = .pause
|
|
|
+ player.pause()
|
|
|
+ }else{
|
|
|
+ cell.status = .loading
|
|
|
+ player.play()
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
cell.changeSelected(true)
|
|
|
|
|
|
- chosedCell = cell
|
|
|
+ chosedIndexPath = indexPath
|
|
|
|
|
|
if cell.status == .normal {
|
|
|
if let urlStr = cell.data?.musicPath, let url = URL(string: urlStr){
|
|
|
BFLog(1, message: "歌曲地址: \(url)")
|
|
|
if url.absoluteString != (player.currentItem?.asset as? AVURLAsset)?.url.absoluteString ?? "b" {
|
|
|
+ if player.currentItem != nil {
|
|
|
+ NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
|
|
|
+ }
|
|
|
player.replaceCurrentItem(with: AVPlayerItem(url: url))
|
|
|
+ NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: .main) {[weak self] notice in
|
|
|
+ guard let wself = self else { return }
|
|
|
+ wself.player.currentItem?.seek(to: .zero, completionHandler: nil)
|
|
|
+ wself.choseCell()?.status = .pause
|
|
|
+ wself.chosedCellStatu = .pause
|
|
|
+ }
|
|
|
+
|
|
|
cell.status = .loading
|
|
|
}else {
|
|
|
cell.status = .playing
|
|
@@ -179,6 +215,8 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
player.pause()
|
|
|
cell.status = .pause
|
|
|
}
|
|
|
+ chosedCellStatu = cell.status
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -187,7 +225,7 @@ extension BFMusicSearchController : UITableViewDelegate, UITableViewDataSource {
|
|
|
cell?.status = .normal
|
|
|
cell?.changeSelected(false)
|
|
|
|
|
|
- chosedCell = nil
|
|
|
+ chosedIndexPath = nil
|
|
|
player.pause()
|
|
|
}
|
|
|
|