歌曲更改时,我试图获取通知,但是插入nowPlaying.beginGeneratingPlaybackNotifications
时出现错误MPMediaItem does not have a member named beginGeneratingPlaybackNotifications
。我该如何解决?
let nowPlaying = MPMusicPlayerController.systemMusicPlayer().nowPlayingItem
let playing = MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo
let Title = nowPlaying.valueForProperty(MPMediaItemPropertyTitle) as! String
let Artist = nowPlaying.valueForProperty(MPMediaItemPropertyArtist) as! String
let Album = nowPlaying.valueForProperty(MPMediaItemPropertyAlbumTitle) as! String
let duration = nowPlaying.valueForProperty(MPMediaItemPropertyPlaybackDuration) as! NSNumber
class ViewController: UIViewController {
var ID3 = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
var ID3 = UILabel(frame: CGRectMake(0,0,400,400))
ID3.center = CGPointMake(160, 284)
ID3.textAlignment = NSTextAlignment.Center
ID3.text = (("Song: " + Title) + "\n" + ("Artist: " + Artist) + "\n" + ("Album: " + Album))
ID3.numberOfLines = 3
self.view.addSubview(ID3)
//prints into the console what song is currently playing
println("Song: " + Title)
println("Artist: " + Artist)
println("Album: " + Album)
nowPlaying.beginGeneratingPlaybackNotifications()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "nowPlayingItemIsChanged:",
name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification,
object: nil)
func nowPlayingItemIsChanged(notification: NSNotification){
println("Player state changed")
}
最佳答案
beginGeneratingPlaybackNotifications
是MPMusicPlayerController
上的一种方法。尝试调用MPMusicPlayerController.systemMusicPlayer().beginGeneratingPlaybackNotifications
,并确保在完成后与调用MPMusicPlayerController.systemMusicPlayer().endGeneratingPlaybackNotifications
保持平衡。