Hello @JoeKun,
I would like to know how to play the preview assets using AVFoundation's AVQueuePlayer or AVFAudio's AVAudioPlayer.
It would be really great if you could share a code snnipet.
Regards
Post
Replies
Boosts
Views
Activity
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()
Got It,
Thanks for the superfast reply
Warm Regards
Nitish Kumar