How can I get Song duration from MusicPlayer.Queue.currentEntry

I'm using Music Kit in swiftUI and I try to get duration information from MusicPlayer.queue.currentEntry. But I can't find the solution. How can I get Song duration from MusicPlayer.Queue.currentEntry?

Answered by Frameworks Engineer in 684767022

Hello @LeeJaeho,

Thank you for your interest in MusicKit.

Assuming the MusicPlayer.Queue's currentEntry has an underlying item, then you get the underlying Song's duration or the underlying MusicVideo's duration.

var duration: TimeInterval?
if let currentEntry = player.queue.currentEntry, let item = currentEntry.item {
    switch item {
        case .song(let song):
            duration = song.duration
        case .musicVideo(let musicVideo):
            duration = musicVideo.duration
        @unknown default:
            break
    }
}

// Do something with the `duration`.

I hope this helps.

Best regards,

Accepted Answer

Hello @LeeJaeho,

Thank you for your interest in MusicKit.

Assuming the MusicPlayer.Queue's currentEntry has an underlying item, then you get the underlying Song's duration or the underlying MusicVideo's duration.

var duration: TimeInterval?
if let currentEntry = player.queue.currentEntry, let item = currentEntry.item {
    switch item {
        case .song(let song):
            duration = song.duration
        case .musicVideo(let musicVideo):
            duration = musicVideo.duration
        @unknown default:
            break
    }
}

// Do something with the `duration`.

I hope this helps.

Best regards,

How can I get Song duration from MusicPlayer.Queue.currentEntry
 
 
Q