Skip to content

Commit 0bebc23

Browse files
authored
fix: Fix issue when using AudioSourceType.Microphone (#883)
1 parent 709176b commit 0bebc23

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

com.unity.renderstreaming/Runtime/Scripts/AudioStreamSender.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ class AudioStreamSourceMicrophone : AudioStreamSourceImpl
377377
int m_frequency;
378378
string m_deviceName;
379379
AudioSource m_audioSource;
380+
GameObject m_audioSourceObj;
380381
AudioStreamSender m_parent;
381382

382383
public AudioStreamSourceMicrophone(AudioStreamSender parent) : base(parent)
@@ -414,7 +415,10 @@ IEnumerator CreateTrackCoroutine(WaitForCreateTrack instruction)
414415
// set the latency to “0” samples before the audio starts to play.
415416
yield return new WaitUntil(() => Microphone.GetPosition(m_deviceName) > 0);
416417

417-
m_audioSource = m_parent.gameObject.AddComponent<AudioSource>();
418+
m_audioSourceObj = new GameObject("Audio");
419+
m_audioSourceObj.hideFlags = HideFlags.HideInHierarchy;
420+
DontDestroyOnLoad(m_audioSourceObj);
421+
m_audioSource = m_audioSourceObj.AddComponent<AudioSource>();
418422
m_audioSource.clip = micClip;
419423
m_audioSource.loop = true;
420424
m_audioSource.Play();
@@ -424,7 +428,7 @@ IEnumerator CreateTrackCoroutine(WaitForCreateTrack instruction)
424428

425429
public override void Dispose()
426430
{
427-
if (m_audioSource != null)
431+
if (m_audioSourceObj != null)
428432
{
429433
m_audioSource.Stop();
430434
var clip = m_audioSource.clip;
@@ -434,7 +438,8 @@ public override void Dispose()
434438
}
435439
m_audioSource.clip = null;
436440

437-
Destroy(m_audioSource);
441+
Destroy(m_audioSourceObj);
442+
m_audioSourceObj = null;
438443
m_audioSource = null;
439444
}
440445
if (Microphone.IsRecording(m_deviceName))

0 commit comments

Comments
 (0)