gpt4 book ai didi

ios - UIImageView图像随着不同的歌曲而变化

转载 作者:行者123 更新时间:2023-11-30 11:10:09 25 4
gpt4 key购买 nike

我有一个简单的媒体播放器,我试图让它随着歌曲的变化而改变艺术作品图像。使用我现在拥有的代码,当您点击播放时,它将显示艺术品,但是当我点击下一个按钮跳到下一个项目时,它会保持不变,除非您点击另一个按钮。

如何使 UIImageView 图像随着歌曲媒体项的变化而变化?

import UIKit
import MediaPlayer

class ViewController: UIViewController {
@IBOutlet weak var coverImageView: UIImageView!

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

func showArt(){
coverImageView.image =
myMediaPlayer.nowPlayingItem?.artwork!.image(at: CGSize.init(width: 500, height: 500))
coverImageView.isUserInteractionEnabled = true
}

@IBAction func playButton(_ sender: UIButton) {
togglePlay(on: sender)
showArt()
}

@IBAction func backButton(_ sender: UIButton) {
back()
}

@IBAction func nextButton(_ sender: UIButton) {
skip()
}

}

我的其他功能如下:

import MediaPlayer

let myMediaPlayer = MPMusicPlayerApplicationController.systemMusicPlayer

let playDrake = MPMediaPropertyPredicate(value: "Drake", forProperty: MPMediaItemPropertyArtist, comparisonType: MPMediaPredicateComparison.equalTo)

let myFilterSet: Set<MPMediaPropertyPredicate> = [playDrake]

func createQueue() {
let drakeQuery = MPMediaQuery(filterPredicates: myFilterSet)
myMediaPlayer.setQueue(with: drakeQuery)
}

func skip() {
myMediaPlayer.skipToNextItem()
}

func back() {
if myMediaPlayer.currentPlaybackTime > 0.05 {
myMediaPlayer.skipToPreviousItem()
} else if myMediaPlayer.currentPlaybackTime < 0.05 {
myMediaPlayer.skipToBeginning()
} else {
//do nothing
}
}


func togglePlay(on: UIButton) {
if myMediaPlayer.playbackState.rawValue == 2 || myMediaPlayer.playbackState.rawValue == 0 {
on.setTitle("Pause", for: UIControlState.normal)
myMediaPlayer.play()
} else if myMediaPlayer.playbackState.rawValue == 1{
on.setTitle("Play", for: UIControlState.normal)
myMediaPlayer.pause()
} else {
// do nothing
}
}

最佳答案

尝试异步加载图像

DispatchQueue.global(qos: .background).async {
myMediaPlayer.nowPlayingItem?.artwork!.image(at: CGSize.init(width: 500, height: 500))
}

关于ios - UIImageView图像随着不同的歌曲而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52266469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com