UWP app crashes when plugging/unplugging earphones

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.

Hi,

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

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 : Unity Issue Tracker - Reconnecting audio device doesn't trigger AudioSettings.OnAudioConfigurationChanged

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

using System;
using UnityEngine;

#if NETFX_CORE
using Windows.Media.Devices;
using Windows.Devices.Enumeration;
#endif

public class AudioCrashScript : MonoBehaviour
{
#if NETFX_CORE
    private string _deviceId = string.Empty;

    private void Awake()
    {
        MediaDevice.DefaultAudioRenderDeviceChanged += OnDefaultSoundPlaybackDeviceChanged;
    }

    private async void OnDefaultSoundPlaybackDeviceChanged(object sender, DefaultAudioRenderDeviceChangedEventArgs args)
    {
        DeviceInformation deviceInfo = await DeviceInformation.CreateFromIdAsync(args.Id);
        if (deviceInfo.Id == _deviceId)
            return;

        _deviceId = deviceInfo.Id;

        UnityEngine.WSA.Application.InvokeOnAppThread(() =>
        {
            AudioConfiguration config = AudioSettings.GetConfiguration();
            AudioSettings.Reset(config);
        }, true);
    }
#endif
}

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.

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 -_-

I’ll ask our QA to look at it again.

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.

1 Like