为什么beginGeneratingPlaybackNotifications会创建错误?

时间:2015-08-11 21:12:04

标签: ios swift

我正在尝试在歌曲更改时收到通知,但是当我插入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")

        }

1 个答案:

答案 0 :(得分:0)

beginGeneratingPlaybackNotificationsMPMusicPlayerController上的一种方法。尝试拨打MPMusicPlayerController.systemMusicPlayer().beginGeneratingPlaybackNotifications并确保在您完成后拨打MPMusicPlayerController.systemMusicPlayer().endGeneratingPlaybackNotifications时与之保持平衡。