I have 2 audio sources in my unity scene (this is for HoloLens 2). I want one of the audio sources to use my laptop speakers as the audio output, and the other source to use the HoloLens speakers.
Can someone please help?
So far I have tried using, but this is not working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LaptopOnlyAudio : MonoBehaviour
{
// References to the two audio sources that will play through the laptop
public AudioSource audioSourceLaptop1;
// State to control the playback of the first laptop audio (toggled by spacebar)
private bool isLaptopAudio1Playing = false;
void Start()
{
// Ensure both laptop audio sources are stopped initially
audioSourceLaptop1.Stop();
// Set the audio output to 2D (non-spatialized) for both laptop audio sources
SetAudioSourceOutput(audioSourceLaptop1);
// Disable audio playback on HoloLens at runtime
if (IsRunningOnHoloLens())
{
// Mute audio source on HoloLens
audioSourceLaptop1.mute = true;
}
}
void Update()
{
// Listen for the spacebar key press to toggle the first laptop audio
if (Input.GetKeyDown(KeyCode.Space))
{
ToggleLaptopAudio1();
}
}
// Toggle the first laptop audio on and off
private void ToggleLaptopAudio1()
{
if (isLaptopAudio1Playing)
{
audioSourceLaptop1.Pause();
}
else
{
audioSourceLaptop1.Play();
}
isLaptopAudio1Playing = !isLaptopAudio1Playing;
}
// Set the audio source to non-spatialized (2D) audio for laptop playback
private void SetAudioSourceOutput(AudioSource audioSource)
{
audioSource.spatialBlend = 0f; // Set to 2D for laptop audio (no spatialization)
AudioSettings.SetSpatializerPluginName("None"); // Disable spatialization
}
// Helper function to check if the app is running on HoloLens
private bool IsRunningOnHoloLens()
{
return Application.platform == RuntimePlatform.WSAPlayerX86 ||
Application.platform == RuntimePlatform.WSAPlayerX64 ||
Application.platform == RuntimePlatform.WSAPlayerARM;
}
}