JoshLmao / Spotify4Unity

🎮🎵 Add the SpotifyAPI.NET library into Unity, enabling access to the Spotify Web API, allowing for control of Spotify and accessing the world of music
https://assetstore.unity.com/packages/tools/integration/spotify4unity-ui-tools-spotify-authorization-129028
MIT License
94 stars 8 forks source link

Stream on Unity AudioSource #11

Open bennibayer opened 5 years ago

bennibayer commented 5 years ago

Hi, do you know if (and how) we can play the audio stream on an Unity AudioSource? I just got it to work already with the short 30 seconds preview songs, because they are mp3 files on a defined URL. But the main song seems to have an own "spotify-protocol" (not http), which can only be played in the spotify app or webplayer or similar. But i guess, because these apps can use it, somehow this protocol should also be usable to stream it on Unity.

Or do you know, how we can find out the mp3 in the spotify-backend? I just think, that this is hidden by spotify and they don't allow us to access it. Neither directly per URL nor by the API.

Maybe it would be also an other/easier approach, that we add the app itself to the devices list or similar? So that we simply can select it from there and then somehow redirect it into Unity. Do you maybe have any idea, how this could work?

Or does your plugin already support this feature and i just did not found it yet^^

JoshLmao commented 5 years ago

So my first thought reading this was "Noooooope, not possible". One of the first things I knew when starting to make this was it's impossible to stream the music through an AudioSource. That's explicitly stated in their Web API too. I also put this on the store page since that's a big feature

However, the option to make the app a Spotify "device" and have it come through there is definitely something I will look into. It's on my roadmap for this however I currently have no idea if it is possible or not. I would like to think so since I have made a Raspberry Pi act as a device and can cast my music to it using Raspotify

bennibayer commented 5 years ago

thanks for the response.

ok, i see. Let's hope, that they will add this feature soon :-)

But for now I'm also thinking about different possibilities, because this should be one of the main features in my app. Let you know, if i find something.

Just thought i ask, if you have maybe already a good approach or any experience with it. Maybe running the Raspotify as a background service or similar could be indeed a good idea, thanks.

JoshLmao commented 5 years ago

It depends on what you're actually trying to make. If Spotify allowed people to download and stream the music through their own app, they'd be out of business by now and that's where the problem comes from. I also don't want to create a hack-y way of making it possible because Spotify would just shut me down and sue me

imo, these are the only possibilities. They recently released the Web Playback SDK which supports streaming music through a browser using an encryption extension. But apart from that, the only other option seems to be the one I mentioned before about creating a device

bennibayer commented 5 years ago

Sure, we have to find a legal way. I also don't want to risk, that my app is shutted down.

Ok cool, this Web Playback SDK could maybe work with a webview like e.g. this one They actually say that android is currently not supported yet. But in this webview you can set the user-agent. So we can maybe simulate being on a windows or mac chrome browser.

And this one should be then already inside of the Unity scene. Just not shure, how well it supports audio and sounds.

JoshLmao commented 5 years ago

That seem's like a good combination!

I just had a quick play with it and had a theory on how to get it working. I think you could use the authorization from my Spotify Service and have a "hidden" web view page that streams it. From the demo code they give, you have to manually change to that playback device but that can be done through the Spotify Service too

I'm only skeptical about their "spotify-player.js" not working inside Unity somehow. When I get some time, I will take a look at this myself but feel free to attempt at what I suggested or your own theory on how you could get it working 😉 (and update this if you do!)

bennibayer commented 5 years ago

Cool, thanks for the tips. Yes, i'll keep you updated.

JoshLmao commented 5 years ago

Update: After spending the last couple days investigating this, this is what I've found.

  1. A lot of the Web View packages for Unity don't have the encryption support needed for the Spotify Player. That's how the SDK works. It imports an iframe element to the webpage but if the browser doesn't have Widevine or EME encryption then it won't initialize
  2. It might be possible with this SimpleUnityBrowser (or other web views) however the CEF Browser that the plugin uses requires adding Widevine support
  3. I brought the SimpleUnityBrowser to Github and tried to add it myself on the /develop/ branch but failed. I kept getting "Incompatiable" error with Widevine. If you or anyone wants to try themselves, I would appreciate it 😊 (Just fork or ask me and I can add you as a contributor)

I think I'm going to wait a while and see if Spotify plans on supporting through other methods. I also might come back to trying to add Widevine support but at the moment, I'm kind of burnt out from all the failing 😓

thislookshard commented 4 years ago

Has there any progress on figuring out how to play a song from Spotify through Unity's AudioSource? I see that you'd have to make the app a device, I haven't looked too much in to Spotify's web developer tools myself, but I want to at some point. But if we could play the song's through AudioSource then that'd be awesome and help me out with my rhythm game I'm making for my senior project.

JoshLmao commented 4 years ago

Unfortunately not. The issue still lies with being able to use the Spotify Web Playback SDK, which is fully javascript, inside Unity. Like said before, it's not just getting a browser too, it's also having the Widevine SDK.

bio998 commented 4 years ago

I'd love to know if this is possible, I make music visualisers, and need to get the fft of the sound, but this is currently, as far as I am aware, impossible.

rajshekarreddy97 commented 4 years ago

Any updates on this?