I am trying to play a 30 second preview of a Spotify song using AVAudioPlayer. The url to the mp3 file looks like this: https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2
Here is my code to play this song:
trackObj.previewURL = NSURL(fileURLWithPath:"https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2")
// set up the audio player
if trackObj.previewURL != nil {
do {
try previewPlayer = AVAudioPlayer(contentsOfURL: trackObj.previewURL)
previewPlayer.prepareToPlay()
previewPlayer.play()
} catch _ {}
}
This does not do anything. The song does not begin to play at all.
Try this,
var aSongURL: String = String(format: " https://p.scdn.co/mp3-preview/4f5fa753f8e814c51c718bf34bbc594bf55ef5b2")
// NSLog(@"Song URL : %@", aSongURL);
var aPlayerItem: AVPlayerItem = AVPlayerItem(URL: NSURL(string: aSongURL)!)
var anAudioStreamer: AVPlayer = AVPlayer(playerItem: aPlayerItem)
anAudioStreamer.play()
Avoid syntax mistake if any because i have type here
hope this will help :)