profile picture

Published by Cory

published
updated

Category: Web, HTML, Tech

View Cory's Blog

View Cory's Profile

Report Blog Entry

🖥 Code Snippet: Autoplay Sound Effects

HAH, THERE'S A BETTER WAY!

OK, make a public or unlisted playlist on YouTube and load it with the music you want, then use this code. It is working! Ignore what's below, but I'll leave it for posterity. Thanks to subfuse for figuring this out! Your playlist can be as long as you'd like, and each song can be as long as you'd like, as far as I can tell.

Caveats: Ad blockers need to be completely off, or at least passing though YouTube and SpaceHey. (But we should be allowing ads on SpaceHey anyway just in case, to support An) In my testing, it works in Safari only when a user clicks on the browser anywhere within like the first second of the page loading. (I'm mulling over how to fix that.) Chrome it works better, Edge it works great. Firefox it may not work at all. It will definitely not work on mobile.

<iframe style="opacity:0;position:absolute;pointer-events:none;" frameborder="no" src="https://www.youtube.com/embed/videoseries?list=[YOUR YOUTUBE PLAYLIST ID HERE];&amp;autoplay=1" allowfullscreen=""></iframe></p>



So, I found ufish here on SpaceHey who managed to get an autoplayed sound effect working on their profile, so I'm adapting their code for your use!

BIG HUGE HONKIN' CAVEAT: This only reliably works with YouTube clips that are 10 seconds or less in duration. Also this will not work on mobile, at least not on iPhone.

<iframe src="https://www.youtube.com/embed/[YOUTUBE ID GOES HERE]?&amp;;autoplay=1" frameborder="0" class="hideobj"></iframe>

Then somewhere in your style code, put this to hide it:

.hideobj{ visibility: hidden; height: 20px; width: 0%; }

And then your audio sound effect that's 10 seconds or less will automatically play!


19 Kudos

Comments

Displaying 11 of 11 comments ( View all | Add Comment )

LunaKniteWolfe13

profile picture

Will this work with spotify as well?


Report Comment



I can’t guarantee it’ll work with Spotify.

by Cory ; ; Report

sakon

profile picture

thxxxx


Report Comment

Kathleen

profile picture

have no idea what I'm doing incorrectly, I understand this concept with a singular video...


Report Comment

PhillySoHostile

profile picture

This does not work for me .

I'm on Chrome v88 - 64bit - Win 10 latest update

THIS IS WHAT I HAVE SO FAR:


Report Comment



iframe style="opacity:0;position:absolute;pointer-events:none;" frameborder="no" src="https://www.youtube.com/embed/videoseries?list=https://youtube.com/playlist?list=PL1CBvYw0kxXY5uD2AclKXFi4GGdvLAZEi;&autoplay=1" allowfullscreen="">

by PhillySoHostile; ; Report

you are missing the

by DropDeadNola; ; Report

It doesn't show me what you said.
All your comment says is "You're missing the...." If it was code, it automatically gets deleted from comments.

by PhillySoHostile; ; Report

Josefina

profile picture

Oh my god, this works!!!
I've spent too many (embarrassing) hours trying to figure this out, before I found this code. Bless.
Thank you!


Report Comment

Tyybot

profile picture

Is it possible to have the playlist shuffle?
So to say, every time someone visits your profile, a different song plays?


Report Comment

Rainstorm Moonbow

profile picture

Thanks for this. I find if I add a live music youtube stream it plays different songs all day. Found a nice lo-fi live feed.

Makes my profile take longer to load though, but it's all for the aesthetic. xD


Report Comment



Everything we do is for the aEsThEtIc

by Cory ; ; Report

David Freitag

profile picture

Thank you so much I've almost recreated the profile from when I was 13.


Report Comment

Rainstorm Moonbow

profile picture

& Keeps changing to & a m p ;


Report Comment

FiendOcular

profile picture

Does it still have to be 10 seconds?


Report Comment

ufish

profile picture

heck yeah, im glad you thought this was cool. it should technically work for any embed/iframe supported on spacehey that also has an autoplay function. it's a lil weird tho, like if say you have youtube/soundcloud muted on another tab it won't have sound on your page. also adblockers and mobile aren't always a fan.


Report Comment