• 大小: 54KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C#
  • 标签: c#  麦克风  音量  

资源简介

c#开发的 实时监控麦克风输入音量 转换为百分比显示

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

namespace demo
{
    public partial class Form1 : Form
    {
        private const int SAMPLES = 8;
        private static CaptureDevicesCollection audioDevices;
        private int deviceIndex = -1;
        private Microsoft.DirectX.DirectSound.CaptureBuffer buffer;
        private string deviceName = “没有检测到音频输入设备“;
        private static StringCollection deviceNames;
        private System.Threading.Thread liveVolumeThread;
        private int sampleDelay = 100;
        private int frameDelay = 10;
        private static int[] SAMPLE_FORMAT_ARRAY = { SAMPLES 2 1 };

        public Form1()
        {
            InitializeComponent();
            Initmiclist();
            progressBar.Maximum = Int16.MaxValue + 1;
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }
        /// 
        /// 加载麦克风列表
        /// 

        private void Initmiclist()
        {
            audioDevices = new CaptureDevicesCollection();
            deviceNames = new StringCollection();
            for (int i = 0; i < audioDevices.Count; i++)
            {
                deviceNames.Add(audioDevices[i].Description);
            }
            for (int i = 0; i < deviceNames.Count; i++)
            {
                comboBox1.Items.Add(deviceNames[i].ToString());
                comboBox1.SelectedIndex = 0;
            }
            if (deviceNames.Count < 0)
            {
                comboBox1.Items.Clear();
                comboBox1.Items.Add(deviceName);
            }
        }

        public void Stop()
        {
            if (liveVolumeThread != null)
            {
                liveVolumeThread.Abort();
                liveVolumeThread.Join();
                liveVolumeThread = null;
            }

            if (buffer != null)
            {
                if (buffer.Capturing)
                {
                    buffer.Stop();
                }

                buffer.Dispose();
                buffer = null;
            }
        }

        public void Start()
        {
            Stop();

            if (deviceIndex != -1)
            {
                // capture 对象 捕获麦克风设备
                Capture cap = new Capture(audioDevices[deviceIndex].DriverGuid);
                // 创建对 缓冲区信息 的描述
                CaptureBufferDescription desc = new CaptureBufferDescription();
                WaveFormat wf = new WaveFormat();
                wf.BitsPerSample = 16;
                wf.SamplesPerSecond = 44100;
                wf.Channels = 2;
                // 数据的最小的原子单元
                wf.BlockAlign = (shor

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      12288  2013-12-12 17:29  demo\demo\bin\Debug\demo.exe

     文件      28160  2013-12-12 17:29  demo\demo\bin\Debug\demo.pdb

     文件      21472  2013-12-12 17:29  demo\demo\bin\Debug\demo.vshost.exe

     文件        490  2010-03-17 22:39  demo\demo\bin\Debug\demo.vshost.exe.manifest

     文件       4236  2013-12-12 14:37  demo\demo\demo.csproj

     文件       6307  2013-12-12 17:53  demo\demo\Form1.cs

     文件       3754  2013-12-12 15:53  demo\demo\Form1.Designer.cs

     文件       5817  2013-12-12 15:53  demo\demo\Form1.resx

     文件        826  2013-12-12 17:29  demo\demo\obj\Debug\demo.csproj.FileListAbsolute.txt

     文件        847  2013-12-12 15:53  demo\demo\obj\Debug\demo.csproj.GenerateResource.Cache

     文件      25462  2013-12-12 17:20  demo\demo\obj\Debug\demo.csprojResolveAssemblyReference.cache

     文件      12288  2013-12-12 17:29  demo\demo\obj\Debug\demo.exe

     文件        180  2013-12-12 15:53  demo\demo\obj\Debug\demo.Form1.resources

     文件      28160  2013-12-12 17:29  demo\demo\obj\Debug\demo.pdb

     文件        180  2013-12-12 14:37  demo\demo\obj\Debug\demo.Properties.Resources.resources

     文件        789  2013-12-12 15:08  demo\demo\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7062  2013-12-12 17:09  demo\demo\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        485  2013-12-12 14:18  demo\demo\Program.cs

     文件       1346  2013-12-12 14:18  demo\demo\Properties\AssemblyInfo.cs

     文件       2860  2013-12-12 14:18  demo\demo\Properties\Resources.Designer.cs

     文件       5612  2013-12-12 14:18  demo\demo\Properties\Resources.resx

     文件       1091  2013-12-12 14:18  demo\demo\Properties\Settings.Designer.cs

     文件        249  2013-12-12 14:18  demo\demo\Properties\Settings.settings

     文件        902  2013-12-12 14:18  demo\demo.sln

    ..A..H.     30208  2013-12-12 17:53  demo\demo.v11.suo

     目录          0  2013-12-12 14:18  demo\demo\obj\Debug\TempPE

     目录          0  2013-12-12 14:24  demo\demo\bin\Debug

     目录          0  2013-12-12 17:29  demo\demo\obj\Debug

     目录          0  2013-12-12 14:18  demo\demo\bin

     目录          0  2013-12-12 14:18  demo\demo\obj

............此处省略6个文件信息

评论

共有 条评论