如何防止歌曲项被选中后重新开始?

How to prevent song item to start over again after being selected?

我有一个 collection 视图,其中包含几首歌曲,选择这些歌曲后,该应用会进入播放器 VC,其中有暂停按钮和歌曲图像。 例如,如果用户选择歌曲 a,音乐开始播放,所以“player.play()”为真,然后用户被移动到播放器 VC 及其图像等,现在说用户离开这个播放器 VC 并想要 return给它暂停歌曲,整首歌曲重新开始。我怎样才能防止这种情况发生?我知道我必须执行一个 If 语句来做某事是“player.isPlaying”,但不知道要放入什么。

这是class初始化音频的地方

class SoundManager {
    public var position = 0
    public var songs: [Song] = []
    var player: AVAudioPlayer?    
    func playNavPlayer() {
        let song = songs[position]
        let urlString = Bundle.main.path(forResource: song.trackName, ofType: "mp3")
        do {          
            try AVAudioSession.sharedInstance().setMode(.default) ///app needs to know what mode we are working on
            try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
            guard let urlString = urlString else { return }
            player = try AVAudioPlayer(contentsOf: URL(string: urlString)!)
            guard let player = player else {return}
            player.volume = 0.5
            // player.play()
            // player.delegate = self
            player.numberOfLoops = -1
        } catch {
            print ("Error")
        }
    }    
}

更新单例Class

 class SoundManager {
       private init(){ } 
       static let shared = SoundManager()
       public var position = 0
       public var songs: [Song] = []
       var player: AVAudioPlayer?
       var songURL: URL!
    
       func playNavPlayer() {
           let song = songs[position]
           let urlString = Bundle.main.path(forResource: song.trackName, ofType: "mp3")

           do {
            
           try AVAudioSession.sharedInstance().setMode(.default) ///app needs to know what mode we are working on
           try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)

            guard let urlString = urlString else { return }
        
            player = try AVAudioPlayer(contentsOf: URL(string: urlString)!)
        
            guard let player = player else {return}
            player.volume = 0.5
            player.play()
            //player.delegate = self
            player.numberOfLoops = -1
            
            }
         
         catch {
             print ("Error")
         }

       }

这个class怎么叫

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
      
      //present player
    let position = indexPath.item

    
    guard let vc = storyboard?.instantiateViewController(identifier: "player") as? PlayerViewController else {
          return
      }
    
            func push() {
           
            vc.songs = songs
            vc.position = position
            vc.soundManager = SoundManager.shared
            navigationController?.pushViewController(vc, animated: true)     
    }
      SoundManager.shared.songs = songs
    SoundManager.shared.position = position
    if SoundManager.shared.player?.isPlaying == true && SoundManager.shared.player?.url (This is where I need to see compare song playing with song clicked) {
        push()
    }
    else {
        SoundManager.shared.playNavPlayer()
        push()
    }
    SoundManager.shared.setupRemoteTransportControls()
    SoundManager.shared.setupNowPlaying()
    
    //songs
    
 }

歌曲宣言

 
         var songs = [Song]()

         func configureSongs() {
        
          songs.append(Song(name: "Empire", imageName: "cover1", trackName: "Empire"))
          songs.append(Song(name: "FirstSample", imageName: "cover2", trackName: "FirstSample"))
          songs.append(Song(name: "TBH", imageName: "cover3", trackName: "TBH"))
          songs.append(Song(name: "Trials", imageName: "cover4", trackName: "Trials"))
          songs.append(Song(name: "Magic Spells", imageName: "cover5", trackName: "MagicSpells"))
          songs.append(Song(name: "Still Goin", imageName: "cover6", trackName: "StillGoin"))
        
     }
   Song, is a struct
     import Foundation


      struct Song {
    
         let name: String
         let imageName: String
         let trackName: String
     }

所以 Song 结构应该符合 Equatable 协议,以便我们使用一个函数来比较 Song 结构的两个实例,就像这样

    struct Song: Equatable {
    
         let name: String
         let imageName: String
         let trackName: String
         static func == (lhs: Self, rhs: Self) -> Bool {
         return lhs.trackName == rhs.trackName
    }
     }

然后当我们在 didSelect 中 select 一首歌曲(项目)时,我们实现一个 if 函数来检查播放器是否正在播放并比较两个曲目名称,正在播放的曲目和正在播放的曲目我们只是点击,然后如果为真,我们在大括号中指定我们想要做什么。

     
        if SoundManager.shared.player?.isPlaying == true && songs[position] == SoundManager.shared.songs[SoundManager.shared.position] {
            
            //Whatever you want to do
            
        }