• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5365
  • Last Modified:

Using an AxWindowsMediaPlayer in C#

I need to turn the mute to off on Windows XP, using C# and suspect that I need to instantiate an object similar to AxWindowsMediaPlayer.

Could someone please tell me what object in C#.NET I need to create for this and how to work with it??

thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 4
  • 3
1 Solution
 
monarch_ilhanCommented:
Use waveOutSetVolume function of winmm library.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace VolumeControl
{
   public partial class Form1 : Form
   {
      [DllImport("winmm.dll")]
      public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
 
      [DllImport("winmm.dll")]
      public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
 
      public Form1()
      {
         InitializeComponent();
         // By the default set the volume to 0
         uint CurrVol = 0;
         // At this point, CurrVol gets assigned the volume
         waveOutGetVolume(IntPtr.Zero, out CurrVol);
         // Calculate the volume
         ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
         // Get the volume on a scale of 1 to 10 (to fit the trackbar)
         trackWave.Value = CalcVol / (ushort.MaxValue / 10);
      }
 
      private void trackWave_Scroll(object sender, EventArgs e)
      {
         // Calculate the volume that's being set
         int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
         // Set the same volume for both the left and the right channels
         uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
         // Set the volume
         waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
      }
   }
}

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
I won't need to present a scroll bar to change the volume, I just need to turn mute off before trying to play sound.  So I am trying to make an object for that purpose.  But I understand most (or all) of the object I included may be needless.

I started with your code and created a variabe for currentVolume to substitue for the variable trackWave.Value, but needed to typcast the uint to an int, but think that would totally change the values. Please take a look.
But all I really want is to turn mute off, and don't really think I need to be working with the volume.

Please take a look and let me know.  I have an urgent need to keep the mute off for a management system where the mute keeps getting set to on.

Thanks for the help,
newbieweb

    public partial class AdjustVolume
    {
        [DllImport("winmm.dll")]
        public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);

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

        uint currentVolume = 0;

        public AdjustVolume()
        {
            // By the default set the volume to 0
            uint CurrVol = 0;
            // At this point, CurrVol gets assigned the volume
            waveOutGetVolume(IntPtr.Zero, out CurrVol);
            // Calculate the volume
            ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
            // Get the volume on a scale of 1 to 10 (to fit the trackbar)
            currentVolume = (uint)CalcVol / (ushort.MaxValue / 10);
        }

        public void SetVolume()
        {
            // Calculate the volume that's being set
            int NewVolume = ((ushort.MaxValue / 10) * (int)currentVolume); <<<<< typecasted it to int
            // Set the same volume for both the left and the right channels
            uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
            // Set the volume
            waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
        }
    }
0
 
monarch_ilhanCommented:
waveOutSetVolume(IntPtr.Zero, 0); mutes sound.. Do not waste time for type casting. it is Zero (0) for mute
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
newbiewebSr. Software EngineerAuthor Commented:
I want to unmute sound.

Is that what this does:

            waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);

If so, please show me the code without typcasting to accomplish this.

thanks,
newbieweb
0
 
newbiewebSr. Software EngineerAuthor Commented:
what's the type of :

trackWave.Value

??

I am trying to create a variable inside the object to preseve the value returned by:

CalcVol / (ushort.MaxValue / 10);

0
 
newbiewebSr. Software EngineerAuthor Commented:
Don't worry about my past two questions.  I have you code compiling.  BUT the mute does not come off when I explicitly set the Mute All option in Windows.

My program somehow causes the mute All to become active, and I want my program to remove the Mute.

I hope this cal would do that:

waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);

but it does not.  The value for NewVolumeAllChannels is:
4294639610

Thanks for the help,
newbieweb
0
 
monarch_ilhanCommented:
the below link is exactly what you want. (more than it) download sample code and get necessary part inside it

http://msdn2.microsoft.com/en-us/library/aa719104(VS.71).aspx

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now