Search Unity

Selecting the audio output device

Discussion in 'Audio & Video' started by KBergauer, Mar 4, 2015.

  1. KBergauer

    KBergauer

    Joined:
    Aug 27, 2014
    Posts:
    6
    Hi everyone,

    it always infuriates me when I cannot set a game's audio output device aside from changing my system default. Is there a way to set the audio device Unity is playing to from script (using C#, native plugins or some other method) to enable players to choose a device other than their system default? I have not been able to find any Unity API methods to change the device, only the current configuration (see AudioSettings). Using basic C# I haven't been able to find anything useful yet either. I would be very thankful for any hints on how to accomplish this.

    Yours sincerely,
    KB
     
  2. KBergauer

    KBergauer

    Joined:
    Aug 27, 2014
    Posts:
    6
    Is it simply not possible to change the playback device Unity is using in any way? Games like Guild Wars 2 and programs like Media Player Classic can do it, then again that's neither Unity nor C#. I've yet to discover any way to change the audio device Unity is using. All I ever find are references on how to change Window's default audio device which is certainly not the behaviour I am looking for.

    I appreciate every hint or tip you can provide!
     
  3. Marionette

    Marionette

    Joined:
    Feb 3, 2013
    Posts:
    349
    Are you talking about the ability to have a selection screen with the different devices for your users? I know portaudio and rtaudio both have that ability for listing the devices and setting them, but to be honest, I'm not sure unity exposes that. I too would be interested as I have multiple devices as well.
     
  4. KBergauer

    KBergauer

    Joined:
    Aug 27, 2014
    Posts:
    6
    Yes, it's exactly what I'm looking to do: something like a combobox containing all possible audio devices with the ability to select one to use for Unity audio output. I will look into portaudio and rtaudio, thanks for mentioning them. Maybe they can be incorporated into Unity as a plugin.
     
  5. dshankar

    dshankar

    Joined:
    Feb 28, 2015
    Posts:
    21
    Did you find a solution?

    I need to list the audio output devices in Unity, and let the user chose which device Unity should use for audio output (i.e. NOT the default audio output device selected in Windows)

    Worst case, I assume I have to write a C++ native plugin and do some Windows API calls, and hook that up to a Unity UI in C#.
     
    forestrf likes this.
  6. P_W_Studios

    P_W_Studios

    Joined:
    May 24, 2013
    Posts:
    3
    Any word on a solution to this? There's not too much documentation out there, seems like it really hasn't been done with Unity yet??
     
  7. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Hi all,

    I did some preliminary testing with the FMOD package I'm using in AudioStream ( link in my signature ) and it seems to be correctly playing the audio on an user selected device/output present in the system.

    Note since Unity atm always outputs to system's default device, it's not possible to play on selected device (other than default) via AudioSource - meaning you'll loose all capabilities of manipulating the sound such as changing volume, pitch, spatialization, effecting etc. from the Editor/scripts - it completely sidesteps the Unity audio system.

    I'd have to also come up with some kind of Editor integration since for FMOD to report all devices/drivers the system has to be initialized first - - as a workaround the user might be able to choose the output device only in runtime for now.

    Simple playback of a single file or a stream ( which was the package originally intended for ) should work without problems.

    I'd have to allocate some time for this, but PM me if you are interested in testing and seeing whether things would work with your setup.
     
  8. StudioEvil

    StudioEvil

    Joined:
    Aug 28, 2013
    Posts:
    66
    Nice @r618 !
    I'm struggling to understand how to select and initialize audio devices with low level api within unity in c#. Do you have a little reference for that?

    Thank you, Christian
     
  9. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    look for setDriver call

    edit: also look for the examples for low level API which come with FMOD Studio install

    Cheers!
     
    Last edited: Aug 5, 2016
  10. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Hi,
    so if anybody is interested the new version of AudioStream got approved on the store with the ability to use any available audio output present in the system (for non AudioSource audio/streams EDIT: can handle AudioSource now, too).
    Maybe somebody will find it useful.
     
    Last edited: Jun 20, 2017
    sonofbryce likes this.
  11. StudioEvil

    StudioEvil

    Joined:
    Aug 28, 2013
    Posts:
    66
    Fyi we solved the problem of "selecting audio device on linux platform" using FMod and bypassing Unity's audio system.
     
    sonofbryce and r618 like this.
  12. Taylor-Libonati

    Taylor-Libonati

    Joined:
    Jan 20, 2013
    Posts:
    16
    2018 and still don't see a solution for this in the Unity API
     
  13. horatiu665

    horatiu665

    Joined:
    Nov 22, 2012
    Posts:
    24
    I'm on Unity 2018.2.12f1 and it finally works to change default audio device in windows from toolbar icon => sound output in unity changes to that device. I still don't know API that does this, but I'm happy that this basic thing finally works (it didn't use to)
     
  14. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Yes, Unity now properly responds to notifications about system default audio device changes
    but note this is something rather different than the OP was about
     
  15. khoowaikeong

    khoowaikeong

    Joined:
    Jun 13, 2013
    Posts:
    18
    does unity allow non-default device with current build? i could manually changing output in the new windows 10 mixer works, but once the app is restarted then it goes back to default device and had to be manually set again... this is very streamer unfriendly... it would be great if there is a way to make a select device in game option... :-\
     
  16. manuelgoellnitz

    manuelgoellnitz

    Joined:
    Feb 15, 2017
    Posts:
    397
    So there is still no solution to that problem other then using a completely different api? That is somewhat sad...
     
  17. paulksi

    paulksi

    Joined:
    Nov 9, 2015
    Posts:
    27
  18. Nida21

    Nida21

    Joined:
    Jul 1, 2021
    Posts:
    1
    Hi Everyone,
    Is there any other way to change playback devices on runtime other than audio stream and FMOD?

    Any help will appreciate.
    Thankx in advance
     
    Last edited: Jan 4, 2022
  19. shaepe

    shaepe

    Joined:
    Nov 27, 2021
    Posts:
    2
    Hi folks,

    sorry to warm this up once again in 2023, but we are currently facing a project in which we need to explicitely output audio using the earphone speakers on mobile devices - both Android and iOS.

    Anybody having ideas/workarounds despite spending the effort to integrate AudioStream and/or FMOD?

    Would be awesome to hear some experiences & thanks in advance :)
     
  20. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    416
    Unity uses the AVAudioSession class to interact with iOS's audio session. The audio session controls the audio behavior of the app. To route the audio to the earpiece you would need to set the AVAudioSession's category to AVAudioSessionCategoryPlayAndRecord with the AVAudioSessionCategoryOptionDefaultToSpeaker option. This is not directly accessible via Unity and would require a native plugin to make these settings.

    For Android, I've been told that this could work if permissions are set correctly in your manifest:
    Code (CSharp):
    1. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    2. AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    3. AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
    4.  
    5. string serviceName = activity.GetStatic<string>("AUDIO_SERVICE");
    6. AndroidJavaObject audioManager = context.Call<AndroidJavaObject>("getSystemService", serviceName);
    7.  
    8. audioManager.Call("setMode", 3); // AudioManager.MODE_IN_COMMUNICATION
    9. audioManager.Call("setSpeakerphoneOn", false);
    10.  
     
  21. ChinaXue

    ChinaXue

    Joined:
    Apr 12, 2014
    Posts:
    2