如何在swift中定义MPMediaItemPropertyArtwork?

时间:2015-04-30 19:07:53

标签: ios xcode swift loops boolean

我想添加在锁定屏幕上显示的封面图片。我该怎么做?我尝试了MPMediaItemPropertyArtwork但没有显示任何内容。是否存在我无法看到的覆盖方法并阻止锁定屏幕封面图像?

导入UIKit 导入AVFoundation 导入MediaPlayer

class ViewController: UIViewController {


    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var playButton: UIButton!
    var player:AVPlayer = AVPlayer(URL: NSURL(string: "http://www.liveatc.net/play/ltba.pls"))

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        statusLabel.text = "Status: Not Playing"
        playButton.setTitle("Play", forState: UIControlState.Normal)



        if NSClassFromString("MPNowPlayingInfoCenter") != nil {
            let image:UIImage = UIImage(named: "playerBg")!
            let albumArt = MPMediaItemArtwork(image: image)





            var songInfo: NSMutableDictionary = [
                MPMediaItemPropertyTitle: "LiveATC",
                MPMediaItemPropertyArtist: "Istanbul Ataturk - LTBA",
                MPMediaItemPropertyArtwork: albumArt
            ]







            MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as [NSObject : AnyObject]
        }
        if (AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)) {
            println("Receiving remote control events")
            UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        } else {
            println("Audio Session error.")
        }



//        if Reachability.isConnectedToNetwork() == true {
//            println("Internet connection OK")
//        } else {
//            println("Internet connection FAILED")
//            var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
//            alert.show()
//        }





        switch Reachability.isConnectedToNetwork() {
        case false :
            println("Internet connection FAILED")
            var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
            alert.show()
            fallthrough
         default:
             println("Internet connection OK")


        }


    }




    @IBAction func sliderValueChanged(sender: UISlider) {
        var currentValue = Float(sender.value)
        println(currentValue)
        player.volume = currentValue
    }







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


    @IBAction func buttonPressed(sender: AnyObject) {
        toggle()
    }


    func toggle() {
        if playButton.titleLabel?.text == "Play" {
            playRadio()
            println("Playing")
            statusLabel.text = "Status: Playing"
        } else {
            pauseRadio()
            println("Paused")
            statusLabel.text = "Status: Paused"
        }
    }

    func playRadio() {
        player.play()
        playButton.setTitle("Pause", forState: UIControlState.Normal)

    }

    func pauseRadio() {
        player.pause()
        playButton.setTitle("Play", forState: UIControlState.Normal)
    }

    override func remoteControlReceivedWithEvent(event: UIEvent) {
        if event.type == UIEventType.RemoteControl {
            if event.subtype == UIEventSubtype.RemoteControlPlay {
                println("received remote play")
                playRadio()
            } else if event.subtype == UIEventSubtype.RemoteControlPause {
                println("received remote pause")
                pauseRadio()
            } else if event.subtype == UIEventSubtype.RemoteControlTogglePlayPause {
                println("received toggle")
                toggle()
            }
        }
    }
}

0 个答案:

没有答案