Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UWP app crashes when plugging/unplugging earphones

Discussion in 'Windows' started by DoomSamurai, Jul 26, 2017.

  1. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Hey guys, I've run into an issue with our UWP app where plugging or unplugging a headset or earphones can cause the application to crash. The call stack seems to point out to a problem with Unity.

    0 UnityPlayer!SoundManager::GetHandle soundmanager.cpp

    1 UnityPlayer!SampleClip::LoadBaseSound audioclip.cpp

    2 UnityPlayer!SampleClip::LoadAudioData audioclip.cpp

    3 UnityPlayer!AudioSource:: Play audiosource.cpp

    4 UnityPlayer!AudioSource_CUSTOM_Play audiobindings.gen.cpp


    Has anyone run into this issue before? I'm using Unity 5.6.2f1 and building for Universal 10 using UWP SDK 10.0.14393.0.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Hi,

    this is definitely not expected and it indeed looks like an issue with Unity. Could you file a bug report?
     
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    I've tried to repro this bug in a new Unity project and managed to do it but I don't think it's related to Unity.

    Someone added some code in our project to deal with this issue : https://issuetracker.unity3d.com/is...audiosettings-dot-onaudioconfigurationchanged

    To circumvent this issue, we used the Windows.Media.Devices namespace and subscribed to the MediaDevice.DefaultAudioRenderDeviceChanged event.

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4.  
    5. #if NETFX_CORE
    6. using Windows.Media.Devices;
    7. using Windows.Devices.Enumeration;
    8. #endif
    9.  
    10. public class AudioCrashScript : MonoBehaviour
    11. {
    12. #if NETFX_CORE
    13.     private string _deviceId = string.Empty;
    14.  
    15.     private void Awake()
    16.     {
    17.         MediaDevice.DefaultAudioRenderDeviceChanged += OnDefaultSoundPlaybackDeviceChanged;
    18.     }
    19.  
    20.     private async void OnDefaultSoundPlaybackDeviceChanged(object sender, DefaultAudioRenderDeviceChangedEventArgs args)
    21.     {
    22.         DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(args.Id);
    23.         if (deviceInfo.Id == _deviceId)
    24.             return;
    25.  
    26.         _deviceId = deviceInfo.Id;
    27.  
    28.         UnityEngine.WSA.Application.InvokeOnAppThread(() =>
    29.         {
    30.             AudioConfiguration config = AudioSettings.GetConfiguration();
    31.             AudioSettings.Reset(config);
    32.         }, true);
    33.     }
    34. #endif
    35. }
    36.  

    This code seems to crash the Master UWP app on some machines. Now, if I add a try/catch to the CreateFromIdAsync method, I can prevent the crash.

    My problem now is that when I unplug/plug headphones, I don't have any sound. I'm using Unity 5.6.2f1. Is it possible that the issue I linked in the Unity Issue Tracker is not fixed even though it's marked as fixed? If sound was able to play as expected when using headphones, I would just discard all the code that was added to circumvent the issue.
     
  4. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Disregard all that. I removed all the code that was causing problems and did not call AudioSettings.Reset(config); when changing audio playback device and everything seems to be working now. Sorry about that waste of time -_-
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    I'll ask our QA to look at it again.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Bad news :(. QA managed to reproduce it. Apparently it is device dependent, and it doesn't reproduce on some laptops (that's how it first got marked as fixed). We'll look at it.
     
    DoomSamurai likes this.