Media player, how to get the url of the playing music

Is there a possibility to get the url of the playing song and put it in a global.
I hope that there is something what good works.

My not working example:

      id(song) = lambda 'return {  id(media_player).media_url  };'

The media_content_id of the media_player entity, so in esphome

text_sensor:
  - platform: homeassistant
    id: mediaurl
    entity_id: media_player.whatever
    attribute: media_content_id

Hoi, perfect answer, but…the attribute option don’t work. I have 2 situation tested. One with attribute and one without. In the first one it send the attribute option to HA, but HA don’t gives an answer. In the second situation, without attribute, HA gives an answer: idle. Not the answer I need, but it works. My question: how can I make the attribute option working for the playing title?

Situation 1

   id(song) = id(mediaurl).state;

  - platform: homeassistant
    id: mediaurl
    name: "Song titel"
    entity_id: media_player.huis_player
    attribute: media_content_id


[14:34:32][C][homeassistant.text_sensor:023]: Homeassistant Text Sensor 'Song titel'
[14:34:32][C][homeassistant.text_sensor:024]:   Entity ID: 'media_player.huis_player'
[14:34:32][C][homeassistant.text_sensor:026]:   Attribute: 'media_content_id'
[14:34:32][C][audio:203]: Audio:
[14:34:32][C][audio:225]:   External DAC channels: 1
[14:34:32][C][audio:226]:   I2S DOUT Pin: 22
[14:34:39][V][text_sensor:013]: 'Muziek Text': Received new state leeg

----------------------------------------------------------------------------------------------
Situation 2

  - platform: homeassistant
    id: mediaurl
    name: "Song titel"
    entity_id: media_player.huis_player
 #   attribute: media_content_id


[14:39:05][C][homeassistant.text_sensor:023]: Homeassistant Text Sensor 'Song titel'
[14:39:05][C][homeassistant.text_sensor:024]:   Entity ID: 'media_player.huis_player'
[14:39:05][D][homeassistant.text_sensor:017]: 'media_player.huis_player': Got state 'idle'
[14:39:05][V][text_sensor:013]: 'Song titel': Received new state idle
[14:39:05][D][text_sensor:064]: 'Song titel': Sending state 'idle'
[14:39:05][C][audio:203]: Audio:
[14:39:05][C][audio:225]:   External DAC channels: 1
[14:39:05][C][audio:226]:   I2S DOUT Pin: 22
[14:39:11][V][text_sensor:013]: 'Muziek Text': Received new state leeg

Which integration is the media device?

It’s the esphome media player that works with I2S. That was the reason that I start with getting the song title on the same esp32, see my first post:

               id(song) = lambda 'return {  id(media_player).media_url  };'

Try id(media_player).media_url_
note the final underscore. This is a guess from looking at this

Thank you so far. I tried both option, with and without underscore. The result is the same error.
I think it’s unsolvable and that I must find an another way to solve it. Let’s forget this problem.

The error:

/config/esphome/huis-media.yaml: In lambda function:
/config/esphome/huis-media.yaml:126:31: error: ‘class esphome::i2s_audio::I2SAudioMediaPlayer’ has no member named ‘media_url_’
id(song) = id(player).media_url_;
^~~~~~~~~~
*** [/data/huis-media/.pioenvs/huis-media/src/main.cpp.o] Error 1

Hello @RoelHermus,

I am getting closer with this make_call command that is part of the media_player class just that it returns empty right now! So at least it compiles not sure if anyone else has any suggestions. I do not see any attribute for media_content_id so I thought I could get it from media_player class directly but that seems to be empty. Wrong pointer? IDK but it at least compiles!

Maybe someone can clarify what I am doing wrong?

media player yaml below:

media_player:
  - platform: i2s_audio
    name: Main Livingroom Media Player
    id: main_lr_media_player
    dac_type: external
    i2s_dout_pin: GPIO25
    mode: stereo
    on_play:
      then:
        - lambda: |-
            const optional<std::string> & url = id(main_lr_media_player).make_call().get_media_url();
            if (url.has_value()) {
              ESP_LOGD("main", "  Media URL: %s", url.value().c_str());
            }else{
              ESP_LOGD("main", "  Media URL: empty ");
            }

entities in home assistant:

volume_level: 0.07
is_volume_muted: false
device_class: speaker
friendly_name: LivingRoomESP Main Livingroom Media Player
supported_features: 152077

Any help would be greatly appreciated!!!