吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4867|回复: 4
收起左侧

[C&C++ 转载] C# win7 win10 系统音量控制 CoreAudioApi源码 显示峰值

[复制链接]
wtujoxk 发表于 2018-8-20 14:35
本帖最后由 wtujoxk 于 2018-8-20 15:51 编辑

直接上图:
QQ图片20180820142625.png

[C#] 纯文本查看 复制代码
using System;
using System.Windows.Forms;
 
namespace CoreAudioApi
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        MMDevice device;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化设备
            MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
            device = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        }
 
        /// <summary>
        /// 获取当前音量
        /// </summary>
        public int CurrentVolume
        {
            get => Convert.ToInt32(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100.0f);
        }
        /// <summary>
        /// 设置音量
        /// </summary>
        public int SetVolume
        {
            get => CurrentVolume;
            set
            {
                if (value < 0) device.AudioEndpointVolume.MasterVolumeLevelScalar = 0 / 100.0f;
                else if (value > 100) device.AudioEndpointVolume.MasterVolumeLevelScalar = 100 / 100.0f;
                else device.AudioEndpointVolume.MasterVolumeLevelScalar = value / 100.0f;
            }
        }
        /// <summary>
        /// 定时器更新并显示当前音量和峰值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = "当前音量:"+ CurrentVolume;
 
            //使用Convert.ToInt32转换没有误差
            progressBar1.Value = Convert.ToInt32(device.AudioMeterInformation.MasterPeakValue * 100.0f);
            progressBar2.Value = Convert.ToInt32(device.AudioMeterInformation.PeakValues[0] * 100.0f);
            progressBar3.Value = Convert.ToInt32(device.AudioMeterInformation.PeakValues[1] * 100.0f);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //每点击一次,音量加10
            SetVolume += 10;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            //每点击一次,音量减10
            SetVolume -= 10;
        }
    }
}


github:https://github.com/yuxianqiang/CoreAudioApi

实例:链接:https://pan.baidu.com/s/1qcIFVkwnI_bRRz2B3SXf7A 密码:as4q

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
手柄 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

insisty 发表于 2018-8-20 14:45
感谢分享  
MartinLee 发表于 2018-8-20 14:51
A羽飞 发表于 2018-8-20 15:32
Light紫星 发表于 2020-1-3 01:59
好东西 ,感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 07:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表