How to update UILabel.text after timedMetadata update

Somehow I'm not getting one aspect of xCode working on a mediaplayer.


When timedMetadata is changed the func is executed. I works for audioInfo.nowPlayingInfo. But when I try to update a UILabel on the screen to set the title and artist an error occurs. Updating the UILabel after pressing a button (pushButton) works. Same code. Different results.


override func observeValue(forKeyPath: String?, of: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if forKeyPath != "timedMetadata" { return }
      
        let data: AVPlayerItem = of as! AVPlayerItem
      
        for item in data.timedMetadata! {
          
            if item.commonKey!.rawValue == "title" {
                if let title = item.stringValue, !title.isEmpty {
                  
                    let splitArray = title.components(separatedBy: " - ")
                    artiest = splitArray[0]
                    titel = splitArray[1]
                } else {
                    titel = "Delta Radio"
                    artiest = "Muziek zonder flauwekul!"
                }
            }
          
        }
      
        /
        /
  
        updateMetaData = true
      
        audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: titel, MPMediaItemPropertyArtist:artiest, MPMediaItemPropertyAssetURL: deltaRadioURL!];
      
        updateTitleArtist()
      
    }
  
    @IBAction func pushButton(_ sender: Any) {
      
        self.artiestLabel.text = titel + " / " + artiest
      
    }
  
    func updateTitleArtist() {
      
            self.artiestLabel.text = titel + " / " + artiest
            updateMetaData = false
  
    }


I searched and read about threads. Tried it, but it won't work. Read about programmacilly making a UILabel. Tried it, but it won't work.


Who can help me out? I think I do not grasp some basic concept of xCode?

At this time, just questions.


You set updateMetaData to true or false, but never use it in this part of code.

Where and how is it used ?

You use titel and artiest to update the label.

self.artiestLabel.text = titel + " / " + artiest

It is not clear what is their value when you call updateTitleArtist in override func observeValue

could you print it ?

        audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: titel, MPMediaItemPropertyArtist:artiest, MPMediaItemPropertyAssetURL: deltaRadioURL!];
        print("titel && artiest ", titel, artiest)
        updateTitleArtist()
     
    }

updateMetaData is some leftovercode I wrote in order to check wheter MetaData was updated and then trying to handle update artiestLabel.text. You can ignore that for my question.


I can print it. It prints something like: "Prince / 1999" on the screen after the push on the button. I like to update it automatically. But calling updateTitelArtist in that part of the code gives an error. The object self.artiestLabel is nil!?

I added:


if self.artiestLabel.text != nil {self.artiestLabel.text = titel + " / " + artiest}


Now it works?!


Any idea why?

Just a possibility.


When you call self.artiestLabel.text in updateTitleArtist (from observeValue), the view is not loaded (at least is not).


But when you push button, it is necessarily.


You could check with a test:

       updateMetaData = true
     
        audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: titel, MPMediaItemPropertyArtist:artiest, MPMediaItemPropertyAssetURL: deltaRadioURL!];
       if self.artiestLabel.text == nil { print("It is nil") }
        updateTitleArtist()


If so, breakpoint at updateTitleArtist(), to understand when you are called before view is fully loaded.

Still don't get it, but it works. Somehow 2 cycles of (the same) metaData update are there. The first cycle nil is there so it's ignored. The second cycle works?!

Accepted Answer

Most likely some elements are noy yet loaded when you do the first update, hence it fails as nil.


Don't forget to mark the thread as closed and good luck for continuation.

How to update UILabel.text after timedMetadata update
 
 
Q