MusadoraKit icon indicating copy to clipboard operation
MusadoraKit copied to clipboard

Unable to obtain songs in Library playlist

Open wxyjay opened this issue 1 year ago • 5 comments

Thank you for your efforts. I used the following code to request the playlists and albums in the Library, but the returned data did not include songs. For example: Tracks in the Playlist project is empty.

` let playlist = try await MLibrary.playlist(id: MusicItemID(rawValue: id))

    print(playlist.tracks) <- 

`

Thank you again!

wxyjay avatar Apr 23 '24 07:04 wxyjay

Hi there! Thank you for using MusadoraKit! 🥳

Apple Music usually keeps the response lean, especially for library data. That may be the reason for the tracks being nil.

Here is how you can get the tracks for a playlist:

let playlist = try await MLibrary.playlist(id: "p.kmURBma9pE").with(.tracks)

rudrankriyam avatar Apr 23 '24 07:04 rudrankriyam

Hi there! Thank you for using MusadoraKit! 🥳

Apple Music usually keeps the response lean, especially for library data. That may be the reason for the tracks being nil.

Here is how you can get the tracks for a playlist:

let playlist = try await MLibrary.playlist(id: "p.kmURBma9pE").with(.tracks)

Thanks for the tip, now it works. In recent use, I also found:

The songs in the library returned using code like the following are always empty:

let response = try await MLibrary.songs() <---- Empty

wxyjay avatar Apr 24 '24 05:04 wxyjay

That's interesting. I tried it right now, and it works well. What is the iOS version you tested on? Under the hood, it is using MusicLibraryRequest<Song> for iOS 16.0+

var request = MusicLibraryRequest<Song>()
        request.limit = limit
        let response = try await request.response()
        return response.items

rudrankriyam avatar Apr 24 '24 09:04 rudrankriyam

That's interesting. I tried it right now, and it works well. What is the iOS version you tested on? Under the hood, it is using MusicLibraryRequest<Song> for iOS 16.0+

var request = MusicLibraryRequest<Song>()
        request.limit = limit
        let response = try await request.response()
        return response.items

That's interesting. I tried it right now, and it works well. What is the iOS version you tested on? Under the hood, it is using MusicLibraryRequest<Song> for iOS 16.0+

var request = MusicLibraryRequest<Song>()
        request.limit = limit
        let response = try await request.response()
        return response.items

I spent some time checking, but can't solve it so far. I think it may be that I don't know much about the powerful MusadoraKit, and I must take the time to understand this powerful library.

However, I obtained the songs through other methods of MusadoraKit's MLIbrary, such as:

let librarySongs = try await MLibrary.songsForAlbums().flatMap { $0.items }

This gets the music items in the library normally, and then I sort them by the time they were added, so this is a compromise. 😄

In addition, I found that when I run MusadoraKit's sample SwiftUI project into my device, I can get the music from the library normally. I will now take a look at the specific logic of the sample project.

Sorry I didn't provide my running environment.

iOS 17.41, ipadOS 17.41, tvOS 17.4

Compiled by Xcode 15.3. MusadoraKit version: 4.2.0

Thank you for providing such a powerful library, if I discover additional information in the future, I will report it immediately.

Wish you all the best.

wxyjay avatar Apr 25 '24 01:04 wxyjay

That is indeed weird if the Musadora app is working fine, here is the code:

struct LibrarySongsView: View {
  @State private var songs: Songs = []

  var body: some View {
    SongsView(with: songs)
      .navigationTitle("Songs")
      .task {
        do {
          songs = try await MLibrary.songs()
        } catch {
          print(error)
        }
      }
  }
}

rudrankriyam avatar Apr 28 '24 03:04 rudrankriyam