title: Media Player filtering and prepending album plays album, a random song, and then the album again
body: <p>I am trying to make a music player and currently I am stuck on filtering and prepending an album to the playlist.</p>
  
  <p>What I am trying to do is play music on shuffle and then when a user taps a button continue playing songs only from the album currently playing, when it is finished I want to know its finished so I can change UI elements and then go on to play any and all music from the library.</p>
  
  <p>However, what happens is it will play the rest of the songs from the album THEN when it has exhausted all the songs from that album will play a random song then after that one random song will go back to that album ..go through its entirety and then play random songs. Once in a blue moon after it finishes the album it will just play random songs.</p>
  
  <p>In a singleton I have</p>
  
  <pre><code>func getAllSongs(completion: @escaping (_ songs: [MPMediaItem]?) -&gt; Void) {
  MPMediaLibrary.requestAuthorization { (status) in
    if status == .authorized {
    let query = MPMediaQuery()
    let mediaTypeMusic = MPMediaType.music
    let audioFilter = MPMediaPropertyPredicate(value: mediaTypeMusic.rawValue, forProperty: MPMediaItemPropertyMediaType, comparisonType: MPMediaPredicateComparison.equalTo)
  query.addFilterPredicate(audioFilter)
    let songs = query.items
  
  completion(songs)
  } else {
  completion(nil)
    }
   }
  }
  
  
  func getSongsWithCurrentAlbumFor(item: MPMediaItem) -&gt; MPMediaQuery {
    let albumFilter = MPMediaPropertyPredicate(value: item.albumTitle, forProperty: MPMediaItemPropertyAlbumTitle, comparisonType: MPMediaPredicateComparison.equalTo)
    let predicates: Set&lt;MPMediaPropertyPredicate&gt; = [albumFilter]
    let query = MPMediaQuery(filterPredicates: predicates)
  query.addFilterPredicate(albumFilter)
  return query
   }
  </code></pre>
  
  <p>In my VC to set up the audio I use</p>
  
  <pre><code>let mediaPlayer = MPMusicPlayerApplicationController.applicationMusicPlayer
  
  
  func setUpAudioPlayerAndGetSongsShuffled() {
    try?     AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
    try? AVAudioSession.sharedInstance().setActive(true)
    MBProgressHUD.showAdded(to: view, animated: true)
    MediaManager.shared.getAllSongs { (songs) in
    guard let theSongs = songs else {
    return
   }
  
  self.newSongs = theSongs.filter({ (item) -&gt; Bool in
  return !self.playedSongs.contains(item)
   })
  
  self.mediaPlayer.setQueue(with: MPMediaItemCollection(items: self.newSongs))
  
    self.mediaPlayer.shuffleMode = .songs
   self.mediaPlayer.repeatMode = .none
   self.mediaPlayer.prepareToPlay(completionHandler: { (error) in
  DispatchQueue.main.async {
    MBProgressHUD.hide(for: self.view, animated: true)
  }
    })
    }
    }
  </code></pre>
  
  <p>When the user taps the button to continue playing songs only from that album I use</p>
  
  <pre><code>if let nowPlaying = mediaPlayer.nowPlayingItem {
  let albumQuery = MediaManager.shared.getSongsWithCurrentAlbumFor(item: nowPlaying)
   print("\(albumQuery)")
   let descriptor = MPMusicPlayerMediaItemQueueDescriptor(query: albumQuery)
   mediaPlayer.prepend(descriptor)
    }
  </code></pre>
  
  <p>Upon rereading the documentation I notice it says that I should change to</p>
  
  <pre><code>let mediaPlayer = MPMusicPlayerApplicationController. applicationQueuePlayer 
  </code></pre>
  
  <p>I cannot figure out how to know when an album has been exhausted and then continue playing the rest of the music library.</p>
  
  <p>it would be great to know if the album does not have any other items so that the user could not press the button to play more from the album</p>