0

I am trying to mute the Spotify volume from the Volume Mixer with coreaudio.dll. This is my code:

private float volume = 0.9f;

MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
            AudioSessionManager2 asm = device.AudioSessionManager2;
            SessionCollection sessions = asm.Sessions;
            try
            {
                for (int sid = 0; sid < sessions.Count; sid++)
                {
                    string id = sessions[sid].GetSessionIdentifier;
                    Console.WriteLine(id);
                    if (id.ToLower().IndexOf("spotify.exe") > -1)
                    {
                        if (muted)
                        {
                            volume = sessions[sid].SimpleAudioVolume.MasterVolume;
                            sessions[sid].SimpleAudioVolume.MasterVolume = 0;
                        }
                        else
                        {
                            sessions[sid].SimpleAudioVolume.MasterVolume = volume;
                        }
                        //sessions[sid].SimpleAudioVolume.Mute = muted;


                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

This is the output:

    'VMTit.vshost.exe' (CLR v4.0.30319: VMTit.vshost.exe): Loaded 'C:\Users\Maria\Desktop\Spotify-Ad-Blocker-1.6.3.0\VMTit\VMTit2\bin\Debug\VMTit.exe'. Symbols loaded.
'VMTit.vshost.exe' (CLR v4.0.30319: VMTit.vshost.exe): Loaded 'C:\Users\Maria\Desktop\Spotify-Ad-Blocker-1.6.3.0\VMTit\VMTit2\bin\Debug\CoreAudio.dll'. Cannot find or open the PDB file.
{0.0.0.00000000}.{612a18a0-426d-44a5-bfe5-655ee61c2700}|#%b{A9EF3FD9-4240-455E-A4D5-F2B3301887B2}
{0.0.0.00000000}.{612a18a0-426d-44a5-bfe5-655ee61c2700}|\Device\HarddiskVolume2\Program Files\Steam\Steam.exe%b{00000000-0000-0000-0000-000000000000}
{0.0.0.00000000}.{612a18a0-426d-44a5-bfe5-655ee61c2700}|\Device\HarddiskVolume2\Users\Maria\AppData\Roaming\Spotify\Spotify.exe%b{00000000-0000-0000-0000-000000000000}
The program '[7908] VMTit.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation.

This code used to work perfectly some weeks ago when I was using it. Recently I moved from W8.1 64bit to W8.1 32bits. It's since then that this problem occurs.

Any ideas?

user2530266
  • 287
  • 3
  • 18
  • 1
    Where do you get the exception? Also, why not use the Spotify API instead of manipulating the mixer? – Luaan Mar 24 '16 at 19:45
  • 1
    @Luaan because I didn't want to use any external apis but I just tried it and it looks awesome. Thank you. – user2530266 Mar 24 '16 at 20:17
  • What is the event id you are getting in your event viewer when the crash happens? Since your exceptions are not getting caught by try catch block then it is some serious memory heap corruption issue or something of critical nature. So first you try the step 2 option at this [link](http://stackoverflow.com/questions/7064966/how-to-debug-corruption-in-the-managed-heap) in accepted answer. So first figure out the criticality of the error. If it is something severe like memory heap corruption then even this setting will not be of help. I've assumed you are on .Net 4 or above. – RBT Mar 24 '16 at 20:19

1 Answers1

1

This is a bit of a side-answer, but your best bet is probably to avoid manipulating the mixer, and using the Spotify API instead. It gives you all the control you need, and it will be less confusing for the user - most non-experts I know haven't figured out there's a volume mixer yet and Spotify doesn't check the mixer either, so you end up with a muted Spotify that doesn't look muted, and no way to fix it.

Luaan
  • 62,244
  • 7
  • 97
  • 116