I'm parsing some data from my API and I would like to show artist and title in title section of MPMediaItemPropertyTitle
and the album art. Right now I can print a string, as you can spot in the code below, but I would like to print the API data, as I'm doing it in the labels. Thank you in advance for your help. Here is my code:
import UIKit
import AVKit
import MediaPlayer
class ViewController: UIViewController, AVAudioPlayerDelegate {
var player : AVPlayer!
var dict = NSDictionary()
@IBOutlet weak var artist: UILabel!
@IBOutlet weak var songtitle: UILabel!
@IBOutlet weak var artUrl: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
overrideUserInterfaceStyle = .light
setupRemoteTransportControls()
requestNowPlaying()
setupNowPlaying()
addInterruptionsObserver()
}
[...]
//There is parsing section [...]
DispatchQueue.main.async {
self.songtitle.text = radio.nowPlaying.song.title
self.artist.text = radio.nowPlaying.song.artist
self.playlist.text = radio.nowPlaying.playlist
//albumcover art section
if let artUrl = URL(string: radio.nowPlaying.song.art),
artUrl != self.songArtUrl {
//Loading image from `artUrl`
let imageDatatask = session.dataTask(with: artUrl) { imageData, imageResponse, imageError in
if let imageError = imageError {
print(imageError)
return
}
guard let imageData = imageData else {
print("image_data is nil")
return
}
DispatchQueue.main.async {
self.songArtUrl = artUrl
let albumArt = UIImage(data: imageData)
self.artUrl.image = albumArt
}
}
imageDatatask.resume()
}
[...]
//here is some code with adding remote controls
[...]
func setupNowPlaying() {
// Define Now Playing Info
var nowPlayingInfo = [String : Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "Here I would like to print artist + title"
nowPlayingInfo[MPMediaItemPropertyArtist] = "My name as string - nothing to change"
if let image = UIImage(named: "Deault_albumart") { //Here I would like to add image from API
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: image.size) { size in
return image
}
}
nowPlayingInfo[MPNowPlayingInfoPropertyIsLiveStream] = true
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}