Youtube Music - Media Browser

Seems to be, but I only tested it for a short time - I won’t be using this in the end, since the host media player device can’t skip tracks (like it can if the playlist was cast to it).

Hi, I tried to get ytube music player to work with eather Airfoil (Airplay Receiver) on my Mac and hifiberry an Airplay Receiver on an Raspberry Pi. It did not work. Is it because ytube music player does not support Airplay or is it because of my setup?

Hi, Ytube Player will send a “play this url” to the receiver. This works on mpd and on Google devices but it looks as if this is not supported by many other Mediaplayers out there.
Cheers

1 Like

What has happened to the latest version?
Stopped working asking for a token. Field for cookie taken away in options under config?

Hi, this is a non-beta of the recent oAuth changes. I guess this way more people will see and use it.
To change from header to Oauth simply remove the Integration entries and create new items. Once it is set up this way it should auto refresh the token and never expire anymore

What are the ‘Integration entries’ I need to remove?

Update: Fixed
Was easier than thought. Just pasted the url provided from new entry into my browser - authorised by google - submitted and saved

I upgraded to the latest beta release and loading again the integration all was ok, but suddenly i have this about playlists, sources and speakers:

Schermata 2023-11-22 alle 20.49.24

It’s always on ‘loading’… what happened?

I think I found a work-around for playing youtube music through Sonos. Based on this post: Youtube Music Multi Room Audio with Owntone (Supports both Airplay and chromecasts)

I have set up the hassio-addon-owntone (beta version as the older ‘stable’ one didn’t want to start up its web service) and added the pipe which will be used by OwnTone integration to stream the music from youtube_music_player into:
</s> <s>cd ~/media/music </s> <s>mkfifo librespot-java </s> <s>mkfifo librespot-java.metadata</s> <s> (seems to be not required and even not working as good as the source “Default (no pipe)”)

Then I installed the OwnTone HA integration which automatically detects and connects to the OwnTone Server addon.
OwnTone can now be selected as playback device in youtube_music_player (I needed to restart HA for it to appear) redirecting the stream to that pipe, which will then automatically be picked up by the OwnTone Server addon and start playing.

In the original post I used as as a reference, they would then call an automation to select an output on the OwnTone Server based on room precense. But in my case my Sonos is in a different subnet and I didn’t find immediately how to make OwnTone detect my Sonos. And also the OwnTone Server seems to detect a few other output devices like my amp and kodi instances, but then fails on them with connection timeouts in the logging, not giving me much confidence that I will get it working easily that way.

But OwnTone also supports streaming and exposes http://SERVER_ADDRESS:3689/stream.mp3 for any device to stream from OwnTone.
I currently have only one Sonos device so I just created an automation rule to automatically start streaming OwnTone to my Sonos whenever OwnTone Server would switch to ‘playing’ state:

alias: Play OwnTone stream on Sonos
trigger:
  - platform: state
    entity_id:
      - media_player.owntone_server
    to: playing
action:
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: http://[server IP]:3689/stream.mp3
    target:
      entity_id: media_player.sonos
mode: single

When I now start a playlist on youtube_music_player with OwnTone Server as playback device, OwnTone Server will start playing the stream, hence serving/filling the stream.mp3 file. The automation will kick in and will start playing the stream on my Sonos device. This works fine, with only a few seconds delay between starting a song and actually hearing the song through the speaker.

BUT…
for some reason youtube_music_player only wants to play 1 song from a playlist and then turns itself off at the end of the song.
Also when skipping to the next song manually, it shows the metadata of the next song, but then pauses playback. I have to manually press play again to start that next song. Even when skipping within a song: the progress bar jumps to the position I want to skip to, but then playback pauses and I have to press play again.
In HA logging I see this when skipping to the next track:

2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - Playing track nr 1 / 25
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_url
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_check_api [E]
2023-12-06 11:21:05.723 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - try to find URL on our own
2023-12-06 11:21:05.886 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - self decoded URL via cipher
2023-12-06 11:21:05.995 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_url
2023-12-06 11:21:05.996 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - forwarding url to player media_player.owntone_server
2023-12-06 11:21:05.996 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:06.530 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: off -> paused ... not playing [E]
2023-12-06 11:21:06.530 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: off -> paused [E]
2023-12-06 11:21:06.533 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:06.533 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:06.893 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:06.894 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_track
2023-12-06 11:21:06.894 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] play_media
2023-12-06 11:21:08.550 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> playing ... not playing [E]
2023-12-06 11:21:08.550 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> playing [E]
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_track
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - Playing track nr 2 / 25
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_get_url
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_check_api [E]
2023-12-06 11:21:17.116 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - try to find URL on our own
2023-12-06 11:21:17.270 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - self decoded URL via cipher
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_url
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] - forwarding url to player media_player.owntone_server
2023-12-06 11:21:17.343 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: playing -> paused ... not playing [E]
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: playing -> paused
2023-12-06 11:21:17.768 ERROR (MainThread) [custom_components.ytube_music_player.media_player] media_pause
2023-12-06 11:21:17.771 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:17.771 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.178 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.178 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.180 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.180 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.181 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused ... not playing [E]
2023-12-06 11:21:18.181 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_sync_player ... media_player.owntone_server: paused -> paused [E]
2023-12-06 11:21:18.344 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [S] async_update_extra_sensor [E]
2023-12-06 11:21:18.345 ERROR (MainThread) [custom_components.ytube_music_player.media_player] [E] async_get_track

Any idea what may causing this ? And how this can be fixed?
I’m not sure, but from what I understand from the logging, I think the OwnTone Server switches state to paused when current stream is interrupted (for skipping or for next song) and as a reaction to that state change, youtube_music_player takes over that state and also pauses playback. Is this possibly what happens ?

(also, on a sidenote, there is a small input bug in the youtube_music_player configuration flow: when a proxy is previously set, you are unable to unset the proxy settings by just clearing the fields. It says the configuration is saved, but the cleared proxy fields are reverted when checked again. Replacing the proxy field with a single space seemed to work however)

[EDIT: found out that using the pipe is optional and works less reliable than setting OwnTone source to “Default (no pipe)”]

I did a bit more research, experimenting and rebooting. And I think the OwnTone integration reacts as follows:

  • When a next track or a skip within track is performed in youtube_player, OwnTone server goes from playing → paused.
  • When a track is finished, OwnTone goes from playing → off.

When I execute the off_is_idle service call on youtube_player, it detects when OwnTone Server ends a track, and will start a new track automatically. This seems to work consistently.

Skipping within or to a next song behaves not so consistent. Sometimes it works (with a bit of a delay), sometimes all goes to paused. After a restart of HA it sometimes starts working, sometimes not…

1 Like

So I hacked together a custom card that makes the search feature more of a first class function for me.

gif-2023.12.22-23.17.49

I was wondering if there was already anyone working on something similar.

5 Likes

what card?? send to us

I’ve uploaded my code to GitHub - pathofleastresistor/ytube-media-card. It’s purely experimental and mostly just hacking together what I can get out of the current integration.

Currently is there any change that we can do search for a youtube music song and play the first result and then play randomly as youtube suggest?

I dont want to stick with playlist, day by day, year by year :)))

love this add-on

1 Like

I’d say yes.
Haven’t tried, but you can call the search service call, grab the videoID of the first result and start a radio based on that track.
What have you tried so far?
Jkw

@JKW As I mentioned before, I managed to have ytube_music playing on my Sonos speaker using OwnTone as a ‘proxy’. But I’m still fighting with the next track behaviour of ytube_music:
OwnTone goes to off state at the end of a track, so I set off_is_idle, which works correctly
But when I press next track on ytube_music, OwnTone goes to paused state for a moment, triggering ytube_music to also go to paused state, stopping the stream.
So I think I need an extra service call off_or_pause_is_idle for this to be handled correctly. As next track seems to work correctly when I issue a pause_is_idlecall, but then the next track on end of current track no longer works as off is then no longer detected as idle.

Then another issue I encounter: I also want to be able to play ytube music on other devices than the Sonos/OwnTone combo, but there seems to be no way to reset the the off_is_idle call. Providing an idle_is_idle call to reset the idle detection to default could be a solution to this ? Or by resetting the idle detection method whenever the ytube_music playback target is changed?

Alternatively, and perhaps more user-friendly, but probably more work :slight_smile: , would be that ytube_music keeps track of which idle detection method to use on which target.

Should I open an issue on github for this ?
I’m absolutely new to home assistant and have not much python knowledge, but looking at your code, I think adding an off_or_pause_is_idle and a idle_is_idle service call to your code should lie within my current abilities, so I can try to provide a pull request for that?

What are your thoughts about this ?

I like the idea … actually all of them.
I’d say to provide a list per player might be overkill, I’d rather recommend to have multiple instances of ytubemusicplayer for each playback one, if such special treatment is required.

But idle-is-idle and pause-or-off-is-idle … sure … why not.
Go for the pull request :wink:

Best way would be a harmonizes behavior in Homeassistent … but hey
Jkw

I already have 4 instances (one for each house member, as we have a family account, so everyone has their own YT music account, their own playlists and music preferences (and some even become angry when YT starts giving recommendations based on music choices from other house members, so there is no sharing of accounts possible :-))
I’m using custom:state-switch cards to show the correct YT music card based on currently logged in user. So adding even more instances (for each user on each target) would complicate things even more…

Meanwhile, I tried implementing paused-or-off-is-idle… but found out this is not really a solution. As OwnTone server pauses not only when next track is selected manually, but also when seeking inside a song… now triggering the next track instead of jumping to the selected position in the song. Not sure how to solve that…

Patch the ownTone integration?
Honestly … not sure

That was my first thought also, but looking into the owntone integration, it just calls the owntone api on seek or next/prev song. It seems owntone server itself briefly goes to pause and then start playing again on its own. Hence triggering the state changes in the integration too. Where the YT music integration reacts on the temporary pause state by going into pause, in turn triggering an actual pause on the owntone server by the mediaplayer preventing it from starting to play again automatically…
So I don’t see how to change that behaviour inside the owntone integration; other than possibly trying to ignore owntone state changes that happen within 5s or so.
But that would delay any relevant state change also with 5s.

I’ve been tinkering with your code, and I found a solution that works for me: I added an _ignore_next_remote_pause_state variable and set it to true in async_media_previous_track, async_media_next_track and async_media_seek.
And when a state change playing to paused comes in from the remote player, it is ignored and the variable is reset to false
Result is that when next, prev or seek is performed, owntone pauses, performs the action, then starts playing again and the ytube music integration happily continues and ignores that pause state change. When I now manually press pause on the owntone card, ytube music still also correctly goes into pause.
So I think I solved it with this.

I will prepare a pull request with this.

Hi, I’ve scrolled through this and so many other pages and can’t seem to find anyone that gets as far as i have, without doing steps I have not yet seen (that’s confusing)… my process has been simple to this point (too simple) and probably the reason I’ve not got passed it in weeks.

So here’s the steps:

  1. Download YouTube music player from the HACS store… → Refresh → Yay
  2. Add integrations - it gives me a URL, I go to it, confirm code, login, allow, sweet → done, and go back and click submit:

[it will only let me post one screenshot, have to delete this one and i’ll show the last one, but this is the screen with the URL to open in another tab then come back]

This is where it starts getting different for me to what others appear to get. It takes me to the next screen:

[it will only let me post one screenshot, have to delete this one and i’ll show the last one, but this is the screen with language selection, and header file - same as below but without the error]

I confirm language, I can tick “show advance configuration” but that never does anything. Apart from being able to change the language to anything I don’t understand, my only option is submit. So I click it and then this happens:

It will not open advanced config, I never get to see any fields to enter a name, cookie text, or anything more. it’s as far as I can go and have to cancel and give up, go back to HACS, find it, click the menu and select “Remove”, then restart before trying again and getting the exact same results again.

Now I apologise in advance, because I can only assume i’ve done something stupid on my first attempt (what else will it be right?) because I’m fairly new to HA and still getting used to the dance routine before I start trying to dance the Waltz with it. Thanks in advance, you’re an amazing community and if you can help, that would be fantastic :wink:

Does this still work for you?

I started getting issues with tracks keeping skipping. Something in either HA, OwnTone server or YTube integration changed…

For now I somehow fixed it by making YTube integration to ‘pause’ whenever OwnTone server goes to ‘idle’ (not ‘pause’ like your posts say), it does this every time when I change tracks. Sometimes it still skips a track (I can hear a second of a track before it skips to next one). I’m not sure if OwnTone tries to hit “next” because YTube doesn’t play fast enough.

I could let OwnTone just keep on playing even if YTube is paused, because I can do all control from YTube. It complicates things that both integrations can control playback.

Also, should those commands be done once after HA start or?