Skip to content

Instantly share code, notes, and snippets.

@MartinBspheroid
Created April 6, 2020 13:21
Show Gist options
  • Save MartinBspheroid/b490771af8d1479af65e9f07436aac1c to your computer and use it in GitHub Desktop.
Save MartinBspheroid/b490771af8d1479af65e9f07436aac1c to your computer and use it in GitHub Desktop.

Revisions

  1. MartinBspheroid created this gist Apr 6, 2020.
    89 changes: 89 additions & 0 deletions Application sound control.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using mb;


    //private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);

    namespace SoundTestApp
    {

    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    mb.TaskScheduler.Instance.ScheduleTask(14, 21, 24, () =>
    {
    SetVolume(0);
    });

    mb.TaskScheduler.Instance.ScheduleTask(14, 23, 24, () => { SetVolume(10); });

    }

    private void button1_Click(object sender, EventArgs e)
    {
    Play();
    }
    private void Play()
    {
    string soundFile = @"D:\testFile.wav";
    var sound = new System.Media.SoundPlayer(soundFile);
    sound.PlayLooping();

    }

    private void button2_Click(object sender, EventArgs e)
    {
    Console.WriteLine("Click!");
    SetVolume(0);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    [DllImport("winmm.dll")] private static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
    [DllImport("winmm.dll")] private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);


    /// <summary>
    /// Returns volume from 0 to 10
    /// </summary>
    /// <returns>Volume from 0 to 10</returns>
    public static int GetVolume()
    {
    uint CurrVol = 0;
    waveOutGetVolume(IntPtr.Zero, out CurrVol);
    ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
    int volume = CalcVol / (ushort.MaxValue / 10);
    return volume;
    }

    /// <summary>
    /// Sets volume from 0 to 10
    /// </summary>
    /// <param name="volume">Volume from 0 to 10</param>
    public static void SetVolume(int volume)
    {
    int NewVolume = ((ushort.MaxValue / 10) * volume);
    uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
    waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
    }

    private void button3_Click(object sender, EventArgs e)
    {
    SetVolume(10);
    }
    }
    }