Hi @JoeKun, @dimsumthinking
I just found a way to play the 30 seconds preview using AVFoundation's AVPlayer. I am giving a code sinnpet to help other developers to play the music.
// Explicit Class for soundmanager
class SoundManager : ObservableObject {var audioPlayer: AVPlayer? func playSound(sound: String) {
if let url = URL(string: sound) {
self.audioPlayer = AVPlayer(url: url) }
}
}
var songProfile: Song?
// Fetch a song from MusicCatalogSearchRequest
let song = appleMusicAPI.songs[0]
// Rendering Image from the url
AsyncImage(url: song.artwork?.url(width: 120, height: 120)) .onAppear { self.songProfile = song }
// Add a button to play/pause the song
soundManager.playSound(sound: songProfile?.previewAssets?.first?.url?.absoluteString ?? "")
soundManager.audioPlayer?.play()