MPMediaQuery search for Artists, Albums, and Songs

前端 未结 4 1111
感动是毒
感动是毒 2020-12-15 10:18

How can I search the iPod Library in the same manner as the iOS Music application? I want to make general queries that return results for each Artists, Albums, and Songs. Fo

4条回答
  •  遥遥无期
    2020-12-15 10:43

    Swift 3+ solution for filtering items after running the query, similar to Mohammed Habib's answer:

    let searchString = “string to search”
    let query = MPMediaQuery()
    
    let filteredItems = query.items?.filter { item -> Bool in
        let properties = [item.songTitle,
                          item.artist,
                          item.albumTitle]
    
        let lowercasedProperties = properties.compactMap { $0.lowercased() }
    
        return lowercasedProperties.contains(searchString.lowercased())
    }
    
    print(filteredItems ?? [])
    

提交回复
热议问题