Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Spotify in Unity

Discussion in 'Audio & Video' started by svenvanh, Jan 28, 2021.

Thread Status:
Not open for further replies.
  1. svenvanh

    svenvanh

    Joined:
    Nov 29, 2019
    Posts:
    51
    So I'm working on a game where music is a big factor. And rn I've imported some own music but how cool would it be if I my game can react to the music that is playing in the background (like spotify and youtube music).

    Is there something like device.audio or anything else that would turn the device audio into a audio source?

    Some gameplay (particles react to music)
     
  2. GregoryCollins

    GregoryCollins

    Joined:
    Nov 9, 2021
    Posts:
    1
    i assume device.audio or anything else will turn the device audio into an audio source in my opinion it is correct
     
  3. falkenbrew

    falkenbrew

    Joined:
    Apr 21, 2020
    Posts:
    146
    I'm just trying to do the same and have found a solution.

    Code (CSharp):
    1. void UpdateMic()
    2.     {
    3.         if (currentIndex != inputIndex)
    4.         {
    5.             currentIndex = inputIndex;
    6.             //micClip?.UnloadAudioData();
    7.             if (currentIndex < 0)
    8.             {
    9.                 //micClip = null;
    10.                 if (audioSrc != null)
    11.                 {
    12.                     audioSrc.Stop();
    13.                 }
    14.             }
    15.             else
    16.             {
    17.                 if (audioSrc == null)
    18.                     audioSrc = GetComponent<AudioSource>();
    19.                 if (audioSrc == null)
    20.                     audioSrc = gameObject.AddComponent<AudioSource>();
    21.  
    22.                 string activeDevice = Microphone.devices[currentIndex % Microphone.devices.Length];
    23.                 audioSrc.clip = Microphone.Start(activeDevice, true, 1, 44100);
    24.  
    25.                 int micPos, k = 0;
    26.                 //this supposedly reduces lag
    27.                 do
    28.                 {
    29.                     micPos = Microphone.GetPosition(activeDevice);
    30.                 } while (!(micPos > 0) && k++<100000);
    31.                 Debug.Log("MIC POS " + micPos + " (" + k + ")");
    32.  
    33.                 audioSrc.loop = true;
    34.                 audioSrc.Play();
    35.                 audioSrc.outputAudioMixerGroup = mixer;
    36.             }
    37.         }
    38.     }
    My main problem then was that audio playing in the background is not in synch with what Unity is playing. Slight delay. This took me quite a while to figure out, but I found https://vac.muzychenko.net/en/download.htm (Virtual Audio Cable). If you send Audio from Spotify to Line1 (virtual device) and read/play from this device in Unity you can have everything in sync.

    just found out how to do this about 10min ago, but the audio-quality is reduced and I don't know why yet. maybe you can help me figure this out.
    https://dc5vkde3nc4sk.cloudfront.net/bug/audioRumbling211118_2.mp4
     
  4. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
  5. Ololen

    Ololen

    Joined:
    Nov 6, 2023
    Posts:
    2
    Yes, it is possible to make your game react to the music playing in the background. You can achieve this through audio visualization, which is a technique used to display the elements of sound in a visual manner.
     
Thread Status:
Not open for further replies.