C#控制音量、实现摄像头(荐)

 api控制音量很简单,就是两个方法:

[DllImport("Winmm.dll")]
private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume))//设置音量

[DllImport("Winmm.dll")] 
private static extern uint waveOutGetVolume(int    hwo,out    System.UInt32    pdwVolume); //获取音量

有些地方在两个参数中用的都是long类型,那样会引发错误,因为WinAPI的long类型是32位的,而C#的long是64位的,这就导致堆栈不对称。

第一个参数是波形文件输出设备标识符,可以用0,表示使用首选设备;第二个参数为音量大小,是一个32位的整数,低16位表示左声道的音量,高16位表示右声道的音量,范围从0x0000~0xFFFF。

注意之前还要使用命名空间:

using      System.Runtime.InteropSerices;


现在直接调用这两个方法就可以实现音量的设置和获取了,是不是很简单呢?

不过我们现在是要吧音量控制和c#的trackBar控件联系起来。

看一下下面的方法吧:

//设置音量

   

System.UInt32 Value = (System.UInt32)((double)0xffff * (double)trackBar1.Value / (double)(trackBar1.Maximum - trackBar1.Minimum));//先把trackbar的value值映射到0x0000~0xFFFF范围
   //限制value的取值范围
   if (Value < 0) Value = 0;
   if (Value > 0xffff) Value = 0xffff;
   System.UInt32 left = (System.UInt32)Value;//左声道音量
   System.UInt32 right = (System.UInt32)Value;//右
   waveOutSetVolume(0, left << 16 | right); //"<<"左移,“|”逻辑或运算



//获取音量(基本上像是设置音量的逆,就不多解释了。)

  
 uint v;
   IntPtr p = new IntPtr(0);
   uint i = waveOutGetVolume(p, out    v);
   uint vleft = v & 0xFFFF;
   uint vright = (v & 0xFFFF0000) >> 16;
   trackBar1.Value = (int.Parse(vleft.ToString()) | int.Parse(vright.ToString())) * (this.trackBar1.Maximum - this.trackBar1.Minimum) / 0xFFFF;

转自:http://blog.csdn.net/mchp/article/details/3995970




C#实现摄像头

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;

namespace Camera
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // 枚举所有视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                foreach (FilterInfo device in videoDevices)
                {
                    tscbxCameras.Items.Add(device.Name);
                }

                tscbxCameras.SelectedIndex = 0;
            }
            catch (ApplicationException)
            {
                tscbxCameras.Items.Add("No local capture devices");
                videoDevices = null;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CameraConn();
        }

        private void CameraConn()
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
            videoSource.DesiredFrameSize = new Size(320, 240);
            videoSource.DesiredFrameRate = 1;

            videPlayer.VideoSource = videoSource;
            videPlayer.Start();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            videPlayer.SignalToStop();
            videPlayer.WaitForStop();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            toolStripButton2_Click(null, null);
        }


    }
}


你可能感兴趣的:(object,api,C#,null,Class,winapi)