Git Product home page Git Product logo

Comments (4)

xenolightning avatar xenolightning commented on July 29, 2024

Hmmm, that's a little weird.

Can you post a sample of the code so I can take a look?

from audioswitcher.

mikemeinz avatar mikemeinz commented on July 29, 2024

.NET Framework 4.8.1
4.0.0-Alpha5

To test, I had a slightly modified version of this running at the same time as this version was running. That one changed the volume and muted the audio every 3 seconds. Both in separate instances of Visual Studio 2022 and both run in DEBUG mode.


#define CHECKDEVICE
#define CONTINUOUS
using System;
using System.Collections;
using System.Diagnostics;
using System.Text;
using AudioSwitcher;
using AudioSwitcher.AudioApi;
using AudioSwitcher.AudioApi.CoreAudio;
#if CONTINUOUS
using System.Threading;
#endif

namespace SetSpeaker
{
internal class Program
{
static void Main(string[] args)
{
#if CONTINUOUS
do
{
#endif
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fffff"));
const double StandardAudioVolume = 100.0f; // Max volume
#if CHECKDEVICE
const string TARGETOUTPUT = "Crestron";
CoreAudioDevice itemTarget = null;
CoreAudioDevice itemCurrentDefault = null;
#endif
try
{
#if CHECKDEVICE
var devices = new CoreAudioController().GetPlaybackDevices(); // Get all playback devices
IEnumerator enumerator = devices.GetEnumerator();
while (enumerator.MoveNext())
{
CoreAudioDevice currDev = (CoreAudioDevice)enumerator.Current; // Get current indexed device
Debug.WriteLine($"FullName={currDev.FullName}, Default={currDev.IsDefaultDevice}");
//Console.WriteLine($"FullName={currDev.FullName}, Default={currDev.IsDefaultDevice}");
if (currDev.FullName.Contains("Speaker") || currDev.FullName.Contains("Display Audio"))
{
if (currDev.IsDefaultDevice)
{
// Save the address of the Default playback device
itemCurrentDefault = currDev;
}
if (currDev.FullName.Contains(TARGETOUTPUT))
{
// Save the address of the Target playback device
itemTarget = currDev;
}
}
}
if (itemTarget is null)
{
// If the Target playback device was not found, don't chnage the default playback device
}
else
{
// If the Target sound device is already the default, do nothing.
if (!ReferenceEquals(itemTarget, itemCurrentDefault))
{
itemTarget.SetAsDefault();
Debug.WriteLine($"{itemTarget.FullName} Set As Default");
//Console.WriteLine($"{itemTarget.FullName} Set As Default");
}
}

            if (itemTarget is null)
            { 
                // If the Target playback device was not found, don't change volume settings
            }
            else
            {

#endif
CoreAudioDevice defaultDevice = new CoreAudioController().DefaultPlaybackDevice;
if (defaultDevice.Volume != StandardAudioVolume)
{
defaultDevice.SetVolumeAsync(StandardAudioVolume);
}
if (defaultDevice.IsMuted)
{
defaultDevice.ToggleMuteAsync();
}
#if CHECKDEVICE
if (itemTarget.Volume != StandardAudioVolume)
{
itemTarget.SetVolumeAsync(StandardAudioVolume);
}
if (itemTarget.IsMuted)
{
itemTarget.ToggleMuteAsync();
}
}
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fffff"));
#if CONTINUOUS
Thread.Sleep(4500);
} while (true);
#endif
}
}
}

from audioswitcher.

xenolightning avatar xenolightning commented on July 29, 2024

You are creating a CoreAudioController controller many, many times.

This should only be created once per application (ideally, but a few times here and there is also fine)

It should also be disposed after use

I'd move the creation of the controller outside of your main loop, it should solve the task issue

from audioswitcher.

mikemeinz avatar mikemeinz commented on July 29, 2024

from audioswitcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.