Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

ios - How do I get the album artwork of the currently playing music using swift?

I am trying to create a UIImageView with the album artwork of the song currently playing on a music player. All of the resources I have found are either in objective-c (which I don't know) or do not work (maybe I am not implementing it correctly).

I am currently trying to do this with this code:

class ViewController: UIViewController {

@IBOutlet weak var backgroundAlbum: UIImageView!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    backgroundAlbum.image = MPMediaItemArtwork()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

but I am getting an error in the line

backgroundAlbum.image = MPMediaItemArtwork()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's how I found to do it:

    //Grab the controller
    let sysMP : MPMusicPlayerController & MPSystemMusicPlayerController = MPMusicPlayerController.systemMusicPlayer;

    //Grab current playing
    let currItem : MPMediaItem? = sysMP.nowPlayingItem;

    //Grab currItem's artwork
    let image : UIImage? = currItem?.artwork?.image(at: CGSize(width: 200, height: 200));

Here is a useful reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...