Using Swift to display the name of the current playback .MP3

I was wondering if there is a direct way to display the image and name of the currently playing MP3 file that my music player is playing. My code is as follows and very simple for this example. I just want to test using one .MP3 that I included in my project.

class ViewController: UIViewController {
    let audioPath:NSURL! = NSBundle.mainBundle().URLForResource("SippinOnFire", withExtension: "mp3")

    @IBOutlet var sliderValue: UISlider!
    var player:AVAudioPlayer = AVAudioPlayer()


    @IBAction func play(sender: AnyObject) {

        player.play()
        //println("Playing \(audioPath)")
    }
    @IBAction func pause(sender: AnyObject) {

        player.pause()
    }
    @IBAction func stop(sender: AnyObject) {

        player.stop()
        player.currentTime = 0;
    }
    @IBAction func sliderChanged(sender: AnyObject) {

        player.volume = sliderValue.value

    }

    override func viewDidLoad() {
        super.viewDidLoad()

                 var error:NSError? = nil

        player = AVAudioPlayer(contentsOfURL: audioPath!, error: &error)

        player.volume = 0.5;

    }

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


}

So far, I was able to print the full path to the MP3 file, but could not use the displayed image as a cover for my MP3 in the project or display only the name of the track being played. Can anyone help me with this?

+4
source share
2 answers

  nowPlayingInfo. MPMediaItemPropertyArtwork MPMediaItemArtwork

import MediaPlayer

let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
let audioName = audioPath.lastPathComponent!.stringByDeletingPathExtension
audioInfo.nowPlayingInfo = [ MPMediaItemPropertyTitle: audioName, MPMediaItemPropertyArtist:"artistName"]

mp3, AVPlayerItem commonMetadata.

let playerItem = AVPlayerItem(URL: audioPath)
let metadataList = playerItem.asset.commonMetadata as! [AVMetadataItem]
for item in metadataList {
    if item.commonKey == "title" {
        println("Title = " + item.stringValue)
    }
    if item.commonKey == "artist" {
        println("Artist = " + item.stringValue)
    }
}       

. beginReceivingRemoteControlEvents(), .

override func viewDidLoad() {
    super.viewDidLoad()
    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
}
+10

Leonardo , :

@IBAction func play(sender: AnyObject) {


    let audioInfo = MPNowPlayingInfoCenter.defaultCenter()
println(audioInfo)


    player.play()
    //println("Playing \(audioPath)")


    let playerItem = AVPlayerItem(URL: audioPath)
    let metadataList = playerItem.asset.commonMetadata as! [AVMetadataItem]


    for item in metadataList {
        if let stringValue = item.value as? String {
           println(item.commonKey)
            if item.commonKey == "title" {
                trackLabel.text = stringValue
            }
            if item.commonKey == "artist" {
                artistLabel.text = stringValue
            }

        }
    }
}

, if, , :

            if item.commonKey == "title" {
                trackLabel.text = stringValue
            }
            if item.commonKey == "artist" {
                artistLabel.text = stringValue
            }

!

+1

All Articles