Media Player doesn't play TTS or Media(Wav or MP3)

I’ve set up an ESP32 D1 min as a media player. I’m trying to use TTS specifically, but I would like to play media for some things in the future. The esphome config I’m using is:

i2s_audio:
  - id: i2s_out
    i2s_lrclk_pin: GPIO26
    i2s_bclk_pin: GPIO27

media_player:
  - platform: i2s_audio
    id: media_out
    name: Media Player
    dac_type: external
    i2s_audio_id: i2s_out
    i2s_dout_pin: GPIO23
    mode: mono

I have confirmed the i2s pins and wiring by installing a non-esphome Bluetooth speaker code on the d1 mini, and it works. I am using the current version of esphome, but I have also tried a dev branch because of this thread: Automatically convert TTS audio to MP3 on demand #102814

I’ve tried the TTS from google, Cloud, and Pipper. All 3 TTS service and the MP3 and Wav play through HA via the browser.

This is what is displayed in the ESPHome logs:

[00:29:31][D][media_player:059]: 'Media Player' - Setting
[00:29:31][D][media_player:066]:   Media URL: http://homeassistant:8123/api/tts_proxy/b11aa06073e16ce48220ac67df4e47eacad46636_en-us_-_google_translate.mp3
[00:29:32][W][component:214]: Component i2s_audio.media_player took a long time for an operation (0.60 s).
[00:29:32][W][component:215]: Components should block for at most 20-30ms.
[00:29:32][W][component:214]: Component i2s_audio.media_player took a long time for an operation (0.42 s).
[00:29:32][W][component:215]: Components should block for at most 20-30ms.

If i copy the link to the mp3 file, it does play in the chrome.

I’ve searched this thread and google for answers but have not found anything that works. With so many people getting this to work, I hope I haven’t missed something simple.

Long shot, but try forcing none for the WiFi power_save_mode, see if that makes any difference. I’ve seen it mentioned here and there as causing issues.

re: https://community.home-assistant.io/t/esp32-esphome-and-mediaplayer-mostly-not-playing/625811/5

Thank you for the idea; I haven’t seen that as an issue… Unfortunately, it didn’t work.

I’ve changed the log level to level VERY_VERBOSE
This is the output when I try to send a TTS to the device.

[01:51:43][C][audio:203]: Audio:
[01:51:43][C][audio:225]:   External DAC channels: 1
[01:51:43][C][audio:226]:   I2S DOUT Pin: 23
[01:51:46][D][api:102]: Accepted 10.174.183.100
[01:51:46][VV][api.socket:696]: 10.174.183.100: Handshake complete!
[01:51:46][VV][api.service:522]: on_hello_request: HelloRequest {
  client_info: 'Home Assistant 2023.12.4'
  api_version_major: 1
  api_version_minor: 9
}
[01:51:46][V][api.connection:1103]: Hello from client: 'Home Assistant 2023.12.4' |     10.174.183.100 | API Version 1.9
[01:51:46][VV][api.service:013]: send_hello_response: HelloResponse {
  api_version_major: 1
  api_version_minor: 9
  server_info: 'raspiaudio-muse-proto-267a68 (esphome v2023.12.5)'
  name: 'raspiaudio-muse-proto-267a68'
}
[01:51:46][VV][api.service:531]: on_connect_request: ConnectRequest {
  password: ''
}
[01:51:46][D][api.connection:1121]: Home Assistant 2023.12.4 (10.174.183.100): Connected successfully
[01:51:46][VV][api.service:019]: send_connect_response: ConnectResponse {
  invalid_password: NO
}
[01:51:46][VV][api.service:576]: on_device_info_request: DeviceInfoRequest {}
[01:51:47][VV][api.service:049]: send_device_info_response: DeviceInfoResponse {
  uses_password: NO
  name: 'raspiaudio-muse-proto-267a68'
  mac_address: '94:B5:55:26:7A:68'
  esphome_version: '2023.12.5'
  compilation_time: 'Dec 30 2023, 01:50:05'
  model: 'esp-wrover-kit'
  has_deep_sleep: NO
  project_name: 'raspiaudio.muse-proto'
  project_version: '1.0'
  webserver_port: 0
  legacy_bluetooth_proxy_version: 0
  bluetooth_proxy_feature_flags: 0
  manufacturer: 'Espressif'
  friendly_name: 'RaspiAudio Muse
[01:51:47][W][component:214]: Component api took a long time for an operation (0.05 s).
[01:51:47][W][component:215]: Components should block for at most 20-30ms.
[01:51:47][VV][api.service:585]: on_list_entities_request: ListEntitiesRequest {}
[01:51:47][VV][api.service:062]: send_list_entities_binary_sensor_response:     ListEntitiesBinarySensorResponse {
  object_id: 'action'
  key: 209137191
  name: 'Action'
  unique_id: 'raspiaudio-muse-proto-267a68binary_sensoraction'
  device_class: ''
  is_status_binary_sensor: NO
  disabled_by_default: NO
  icon: ''
  entity_category: ENTITY_CATEGORY_NONE
}
[01:51:47][VV][api.service:316]: send_list_entities_media_player_response:         ListEntitiesMediaPlayerResponse {
  object_id: 'media_player'
  key: 2232357057
  name: 'Media Player'
  unique_id: 'raspiaudio-muse-proto-267a68media_playermedia_player'
  icon: ''
  disabled_by_default: NO
  entity_category: ENTITY_CATEGORY_NONE
  supports_pause: YES
}
[01:51:47][VV][api.service:055]: send_list_entities_done_response:         ListEntitiesDoneResponse {}
[01:51:47][VV][api.service:594]: on_subscribe_states_request: SubscribeStatesRequest {}
[01:51:47][VV][api.service:656]: on_subscribe_homeassistant_services_request:     SubscribeHomeassistantServicesRequest {}
[01:51:47][VV][api.service:070]: send_binary_sensor_state_response:     BinarySensorStateResponse {
  key: 209137191
  state: NO
  missing_state: NO
}
[01:51:47][VV][api.service:683]: on_subscribe_home_assistant_states_request:     SubscribeHomeAssistantStatesRequest {}
[01:51:47][VV][api.service:324]: send_media_player_state_response:     MediaPlayerStateResponse {
  key: 2232357057
  state: MEDIA_PLAYER_STATE_IDLE
  volume: 1
  muted: NO
}
[01:52:03][VV][scheduler:226]: Running interval '' with interval=60000     last_execution=4294943577 (now=36282)
[01:52:23][VV][api.service:558]: on_ping_request: PingRequest {}
[01:52:23][VV][api.service:043]: send_ping_response: PingResponse {}
[01:52:27][VV][api.service:558]: on_ping_request: PingRequest {}
[01:52:27][VV][api.service:043]: send_ping_response: PingResponse {}
[01:52:38][VV][api.service:777]: on_media_player_command_request:     MediaPlayerCommandRequest {
  key: 2232357057
  has_command: NO
  command: MEDIA_PLAYER_COMMAND_PLAY
  has_volume: NO
  volume: 0
  has_media_url: YES
  media_url:     'http://homeassistant:8123/api/tts_proxy/b11aa06073e16ce48220ac67df4e47eacad46636_en    -us_-_google_translate.mp3'
}
[01:52:38][D][media_player:059]: 'Media Player' - Setting
[01:52:38][D][media_player:066]:   Media URL: http://homeassistant:8123/api/tts_proxy/b11aa06073e16ce48220ac67df4e47eacad46636_en-us_-_google_translate.mp3
[01:52:38][W][component:214]: Component api took a long time for an operation (0.05 s).
[01:52:38][W][component:215]: Components should block for at most 20-30ms.
[01:52:38][VV][api.service:324]: send_media_player_state_response: MediaPlayerStateResponse {
  key: 2232357057
  state: MEDIA_PLAYER_STATE_PLAYING
  volume: 1
  muted: NO
}
[01:52:38][W][component:214]: Component i2s_audio.media_player took a long time for an operation (0.59 s).
[01:52:38][W][component:215]: Components should block for at most 20-30ms.
[01:52:39][VV][api.service:324]: send_media_player_state_response: MediaPlayerStateResponse {
  key: 2232357057
  state: MEDIA_PLAYER_STATE_IDLE
  volume: 1
  muted: NO
}
[01:52:39][W][component:214]: Component i2s_audio.media_player took a long time for an operation (0.44 s).
[01:52:39][W][component:215]: Components should block for at most 20-30ms.

From your logs, it appears you are using esp-wrover-kit link for the board config, but you mentioned a D1 Mini, any reason you are not using wemos_d1_mini32 link?

The other thing I can see is timing, barely a second between start and end of play… but you said that same mp3 played in the browser, right? Did you try uploading a plain mp3 file to HA (Media > My Media > MANAGE) and playing that?

service: media_player.play_media
target:
  entity_id: media_player.d1_player
data:
  media_content_id: media-source://media_source/local/mysound.mp3
  media_content_type: audio/mpeg

Do you mind posting the esp32 and esphome config (if the above isn’t the answer)?

Good catch… I don’t know how I missed that… but it didn’t fix it.

it is now:

esp32:
  board: wemos_d1_mini32
  framework:
    type: arduino

esphome:
  name: raspiaudio-muse-proto-267a68
  name_add_mac_suffix: false
  friendly_name: RaspiAudio Muse Proto 267a68
project:
    name: raspiaudio.muse-proto
    version: "1.0"

I have tried to play both a Wav and an MP3 file from the media folder from HA… they both play when the target device is Chrome, but not the device.

One more piece of information… is I can’t get this to work on an M5Stack Atom Echo… if I set the Echo up as a Voice Assistance… it semi works. (semi… it’s not dependable). But TTS or playing media files don’t work.

Did you check the time between MEDIA_PLAYER_STATE_PLAYING and MEDIA_PLAYER_STATE_IDLE in the verbose logs when playing an mp3/wav file? Is it “as expected”?

Check the esphome config against yours for the M5, see if that helps (if you haven’t already).
https://github.com/esphome/firmware/blob/97e1ee17a1ce83cad60f270c066a58302368dd3f/voice-assistant/m5stack-atom-echo.yaml

If you can’t get it to work with either board, but you confirm it works with same wiring/accessories/board with a non-esphome firmware, then I’m at a loss I’m afraid.

Maybe one of the ESPHome wizards can help better than me.

I did check. I’ve used the projects | Media Player from the ESPhome.
I changed the log level to Very_Verbos. Here is the output when trying to play a wav file.

[18:44:32][C][audio:225]:   External DAC channels: 1
[18:44:32][C][audio:226]:   I2S DOUT Pin: 25
[18:44:35][VV][scheduler:226]: Running interval '' with interval=5000 last_execution=29603     (now=34603)
[18:44:35][VV][light.addressable:015]: Addressable Light 'M5Stack Atom Speaker Kit     8a2098' (effect_active=NO)
[18:44:35][VV][light.addressable:019]:   [ 0] Color: R=  0 G=  0 B=  0 W=  0
[18:44:35][VV][light.addressable:021]:  
[18:44:35][VV][api.service:777]: on_media_player_command_request:     MediaPlayerCommandRequest {
  key: 4249557831
  has_command: NO
      command: MEDIA_PLAYER_COMMAND_PLAY
  has_volume: NO
  volume: 0
  has_media_url: YES
  media_url: 'http://homeassistant:8123/media/local/piano-f-a1.wav?authSig=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJlYTE4NmUxMDYwZDc0NDM1OWUyY2E3ZDBiMzFiODg0OCIsInBhdGgiOiIvbWVkaWEvbG9jYWwvcGlhbm8tZi1hMS53YXYiLCJwYXJhbXMiOltdLCJpYXQiOjE3MDM5Nzk4NzUsImV4cCI6MTcwNDA2NjI3NX0.l10kWQJ-vxsQe0ArA
[18:44:35][D][media_player:059]: 'M5Stack Atom Speaker Kit 8a2098' - Setting
[18:44:35][D][media_player:066]:   Media URL: http://homeassistant:8123/media/local/piano-f-a1.wav?authSig=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJlYTE4NmUxMDYwZDc0NDM1OWUyY2E3ZDBiMzFiODg0OCIsInBhdGgiOiIvbWVkaWEvbG9jYWwvcGlhbm8tZi1hMS53YXYiLCJwYXJhbXMiOltdLCJpYXQiOjE3MDM5Nzk4NzUsImV4cCI6MTcwNDA2NjI3NX0.l10kWQJ-vxsQe0ArAIrgzFuOFNWasJxa0G_GuB5YU_E
[18:44:35][W][component:214]: Component api took a long time for an operation (0.09 s).
[18:44:35][W][component:215]: Components should block for at most 20-30ms.
[18:44:36][VV][api.service:324]: send_media_player_state_response:             MediaPlayerStateResponse {
  key: 4249557831
  state: MEDIA_PLAYER_STATE_PLAYING
  volume: 1
  muted: NO
 }
[18:44:36][W][component:214]: Component i2s_audio.media_player took a long time for an operation (0.61 s).
[18:44:36][W][component:215]: Components should block for at most 20-30ms.
[18:44:36][VV][api.service:324]: send_media_player_state_response: MediaPlayerStateResponse {
  key: 4249557831
  state: MEDIA_PLAYER_STATE_IDLE
  volume: 1
  muted: NO
[18:44:36][VV][api.service:324]: send_media_player_state_response:             MediaPlayerStateResponse {
  key: 4249557831
  state: MEDIA_PLAYER_STATE_PLAYING
[18:44:36][VV][api.service:324]: send_media_player_state_response: MediaPlayerStateResponse {
  key: 4249557831
  state: MEDIA_PLAYER_STATE_IDLE

Unless your wav/mp3 file is incredibly short to play (few ms), then this looks weird to me… it’s going straight back to idle in less than a second.

Its 8 sec’s

This is the Arduino code that I run on both the D1 Mini and Atom Echo devices; both work.
I got this code from github HERE

#include "BluetoothA2DPSink.h"
 
BluetoothA2DPSink a2dp_sink;
 
void setup() {
    i2s_pin_config_t my_pin_config = {
        .bck_io_num = 19, ////BCLK  19 for Echo, 27 for D1 Mini
    .ws_io_num = 33,  ////LRC 33 for Echo,  26 for D1 Mini
    .data_out_num = 22, ///Din 22 for Echo, 25 for D1 Mini
    .data_in_num = I2S_PIN_NO_CHANGE
};
a2dp_sink.set_pin_config(my_pin_config);
a2dp_sink.start("MyMusic");
}

void loop() {
}

I didn’t get anywhere trying to trace your logs in the codebase, too many places where it could be failing, not enough info; the i2s_audio stuff seems to be severely lacking debug logging. But I have little doubts it is cancelling play for some reason, according to your logs.

A few suggestions:

Keep your tests to one device, now that you know neither work. The M5 should be easier to debug, since the project builder code is made for that device, compared to the raspi code used on a different model board (just trying to remove potential “unrelated” interference).

Have you tried replacing the media_player component with the speaker component to see if you can get it to play something with voice_assistant TTS (or raw data…)?

Have you tried a file on a different URL than that of HA? Change the below URL to something else if you prefer (this is a 16sec rain sound), just not something hosted on the HA machine.

service: media_player.play_media
target:
  entity_id: media_player.m5_player
data:
  media_content_id: >-
    https://cdn.freesound.org/previews/510/510147_4608521-lq.mp3
  media_content_type: audio/mpeg

It was a DNS look-up issue… my router uses 8.8.8.8 (google) DNS servers… and it doesn’t know about http://homeassistant:8123… and therefore the ESPHome couldn’t download the media file.

Two different fixes fixes here:
1st: Implement a local DNS, and assign a static address with my local DNS. (not ideal in my environment)
2nd: which I’m going to implement: under Setting | System | Network | Home Assistant URL — I changed the Local network from HTTP://homeassistant:8123 to Automatic

This now works on both devices.

1 Like