Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions SWYH/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*
* You should have received a copy of the GNU General Public License
* along with Stream What Your Hear. If not, see <http://www.gnu.org/licenses/>.
*/

*/

using NAudio.CoreAudioApi;

namespace SWYH
{
using OpenSource.UPnP.AV.RENDERER.CP;
Expand Down Expand Up @@ -58,7 +60,7 @@ public partial class App : Application
private System.Windows.Forms.NotifyIcon notifyIcon = null;
private System.Windows.Forms.ToolStripMenuItem streamToMenu = null;
private System.Windows.Forms.ToolStripMenuItem searchingItem = null;
private bool directClose = false; //Skip the statements in Application_Exit function.
private bool directClose = false; //Skip the statements in Application_Exit function.

private void Application_Startup(object sender, StartupEventArgs e)
{
Expand Down Expand Up @@ -193,7 +195,8 @@ private void CloseStreamingConnections()
if (renderer != null && renderer.Connections.Count > 0)
{
var connectionAV = renderer.Connections[0] as AVConnection;
connectionAV.Stop();
connectionAV.Stop();
ToggleMuteWindowsAudioDevices(false);
}
}
}
Expand Down Expand Up @@ -257,9 +260,9 @@ private void streamMenu_RendererSelected(object sender, EventArgs e)
{
var connectionAV = renderer.Connections[0] as AVConnection;
if (rendererItem.Checked)
{
{
rendererItem.Checked = false;
connectionAV.Stop();
connectionAV.Stop();
}
else
{
Expand All @@ -269,10 +272,36 @@ private void streamMenu_RendererSelected(object sender, EventArgs e)
{
connectionAV.Stop();
renderer.CreateConnection(media, DateTime.Now.Ticks);
connectionAV.Play();
connectionAV.Play();
}
}
}
}

public static bool ToggleMuteWindowsAudioDevices(bool muted = false)
{
//Instantiate an Enumerator to find audio devices
NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
//Get all the devices, no matter what condition or status
NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
//Loop through all devices
foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
{
try
{
//Show us the human understandable name of the device
//System.Diagnostics.Debug.Print(dev.FriendlyName);
if (dev.State == DeviceState.Active)
//[Un]Mute it
dev.AudioEndpointVolume.Mute = muted;
}
catch (Exception ex)
{
//Do something with exception when an audio endpoint could not be muted
return false;
}
}
return true;
}

private void connectionAV_OnPlayStateChanged(AVConnection sender, AVConnection.PlayState NewState)
Expand All @@ -285,11 +314,13 @@ private void connectionAV_OnPlayStateChanged(AVConnection sender, AVConnection.P
var item = this.streamToMenu.DropDownItems[sender.Parent.UniqueDeviceName] as System.Windows.Forms.ToolStripMenuItem;
{
if (NewState == AVConnection.PlayState.STOPPED)
{
{
ToggleMuteWindowsAudioDevices(false);
item.Checked = false;
}
else if (NewState == AVConnection.PlayState.PLAYING)
{
{
ToggleMuteWindowsAudioDevices(true);
item.Checked = true;
}
}
Expand All @@ -308,7 +339,8 @@ private void Application_Exit(object sender, ExitEventArgs e)
{
this.notifyIcon.Dispose();
}
}
}
ToggleMuteWindowsAudioDevices(false);
}
}
}
2 changes: 1 addition & 1 deletion SWYH/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class Constants
public const string SWYH_PROCESS_NAME = "SWYH";
public const string RESTART_ARGUMENT_NAME = "--restart";
public const string STREAM_TO_ARGUMENT_NAME = "--streamto:";
public const int NUMBER_OF_RESTART_TEST = 20;
public const int NUMBER_OF_RESTART_TEST = 30;

public const string SWYH_CRASHLOG_FILENAME = "SWYH_crash.log";
public const string REGISTRY_START_SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
Expand Down
Loading