hwyzw 发表于 2025-2-10 13:34:55

Python音量控制教程:轻松变身电脑音量魔法师,实现自动调音

    嗨,朋友们!我再次热情地来到每个人。您是否遇到过如此尴尬的事情?我很专心在办公室工作。突然,计算机发出了一个大声的提醒,这让我所有的同事都吓坏了,我太尴尬了。 ;或者,也许您是在家看电影的令人兴奋的地方,但卷的可怜很低,这非常令人失望。如果计算机可以根据场景自动调整音量,则将更加无忧。不用担心,今天我们将开始一个超级实用的学习旅程,并一起转变为“音量魔术师”。只要设置了这种“智能调音棒”,它就可以帮助我们轻松控制计算机音量。无论是在娱乐过程中静音,还是在娱乐过程中调整音量,都非常强大。这不是超级成功吗?感觉?跟随我,即仍在探索的“初级魔术师”,让我们现在开始吧!

    1。查看“计算机音量控制世界”- 首先了解音量控制的原理

    在拿起智能调音棒之前,我们必须查看计算机音量控制的“神秘领域”。实际上,通过将指令发送到操作系统并更改音频输出设备的参数来实现调整计算机量。就像您对扬声器“稍微调动声音”所说的那样,但是在这里我们使用代码与计算机“通信”。例如,在系统中,有一些特殊的音频接口和功能使我们能够准确地增加和降低音量,或者直接关闭声音,使计算机“哑巴”就像调整无线电旋钮一样。如果我们不知道这些,我们的脚本怎么能知道在哪里施加力以及如何使卷的顺应性?

    提示:打开计算机的音量控制面板,手动调整音量,观察体积栏的变化,并感觉到音量调整过程。一开始,不要被复杂的音频驱动程序和系统API所害怕。专注于如何选择关键操作来控制音量并拉直思想。

    练习问题:观看视频时,请使用系统自己的音量调整功能从最小值调整到最大值的音量,然后将其转回最小值,记录操作步骤,并考虑实现它的困难;然后故意拔下扬声器的插头,然后调整音量,查看系统的反应,并了解硬件对音量调整的影响。

    2。紧紧握住“工具宝藏”- 安装钥匙库

    如果您知道该怎么做,则必须紧紧握住我们的“工具宝藏”才能平稳入门。有一个非常有用的库来控制计算机卷 - 库。它就像卷世界中的“调整大师”一样,它可以深入到基础层并与音频设备进行交互,以帮助我们实现精细的音量调整。和库(对于系统),它用于发出简单的提示声,例如“小铃铛”,它使我们能够测试音量效果。让我们首先导入它们:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">1</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">import</span> pyaudio<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">2</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">import</span> winsound<br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    提示:导入模块时,请注意不要错误地编写名称,否则找不到该工具,并且脚本将无法运行。如果不存在错误报告的模块,则必须检查是否未安装相关的依赖项,或错误地拼写模块名称。

    锻炼问题:故意错误地编写库名,运行代码以查看报告的错误;然后使用.beep(1000,500)功能(发出频率为500毫秒的提示音)来熟悉基本操作。

    3。为“数据基石”奠定基础 - 了解与音量相关的变量

    紧紧握住工具后,我们必须为自动计算机音量调整脚本“奠定基础”,以了解如何使用与音量相关的变量。这是基金会中的基础,与建造房屋一样重要。目标体积值,就像我们想在工作时静音一样,该值为0,这是一个整数int;当前的场景标记,如果我们“工作”,则“工作”是字符串str;调整状态,如果记录一次调整是否成功的卷是 bool。成功是正确的,失败是错误的。变量就像“标记”这些与音量相关的数据,以促进我们找到要使用的信息。

    例如,我们有一个简单的音量调整消息:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">1</span>target_volume = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">50</span>  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 目标音量值,整数型</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">2</span>current_scene = <span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"entertainment"</span>  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 当前场景标记,字符串型</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">3</span>adjustment_status = <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">False</span>  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 调节状态,布尔型</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    如果您被意外地视为字符串变量,例如想拼接其他字符串,您肯定会报告一个错误,并且随后的调整过程也将被弄乱。

    https://img2.baidu.com/it/u=219098408,1825389001&fm=253&fmt=JPEG&app=120&f=JPEG?w=500&h=1031

    提示:代码中的变量名称应该有意义。您可以一目了然地知道存储了哪些数据;定义变量时,您必须确定数据类型,并且切勿选择错误的数据类型。

    练习问题:定义每次调整卷,故意将其初始化为字符串,运行代码并查看数据处理的变量的步长;然后更改变量名称以使其更清晰并熟悉变量操作。

    4.构建“实用要塞”- 自动调整计算机音量脚本以练习

    凭借基础,您必须建造一个“实用的要塞”并开始这样做。

    首先获取当前卷,就像知道说话者的当前“语音大小”一样:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 1</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;"><span style="font-size: inherit;line-height: inherit;">def</span> <span style="font-size: inherit;color: rgb(165, 218, 45);line-height: inherit;">get_current_volume</span><span style="font-size: inherit;color: rgb(255, 152, 35);line-height: inherit;">()</span>:</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 2</span>    pa = pyaudio.PyAudio()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 3</span>    stream = pa.open(format=pyaudio.paInt16,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 4</span>                     channels=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 5</span>                     rate=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">44100</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 6</span>                     input=<span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">True</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 7</span>                     frames_per_buffer=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1024</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 8</span>    data = stream.read(<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1024</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 9</span>    volume = max(abs(byte) <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">for</span> byte <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">in</span> data)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">10</span>    stream.close()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">11</span>    pa.terminate()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">12</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">return</span> volume<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">13</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">14</span>current_volume = get_current_volume()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">15</span>print(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">f"当前音量值: <span style="font-size: inherit;color: inherit;line-height: inherit;">{current_volume}</span>"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    提示:由于许可问题,此步骤可能无法获得卷。您必须确保程序具有适当的音频设备访问权限。有些计算机可能具有多个音频设备,因此您必须选择正确的控制设备。

    练习问题:作为普通用户运行脚本,以查看是否可以获取卷;然后,当计算机连接到多个音频设备(例如耳机和扬声器)时,请尝试获取指定设备的音量并熟悉不同的操作。

    接下来,根据场景调整卷:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 1</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;"><span style="font-size: inherit;line-height: inherit;">def</span> <span style="font-size: inherit;color: rgb(165, 218, 45);line-height: inherit;">adjust_volume</span><span style="font-size: inherit;color: rgb(255, 152, 35);line-height: inherit;">(scene)</span>:</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 2</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">if</span> scene == <span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"work"</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 3</span>        target_volume = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">0</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 4</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">elif</span> scene == <span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"entertainment"</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 5</span>        target_volume = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">80</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 6</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">else</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 7</span>        target_volume = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">50</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 8</span>    pa = pyaudio.PyAudio()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 9</span>    stream = pa.open(format=pyaudio.paInt16,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">10</span>                     channels=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">11</span>                     rate=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">44100</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">12</span>                     output=<span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">True</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">13</span>                     frames_per_buffer=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1024</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">14</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">for</span> _ <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">in</span> range(<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">10</span>):  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 简单模拟调节过程,多次微调接近目标音量</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">15</span>        current_volume = get_current_volume()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">16</span>        <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">if</span> current_volume < target_volume:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">17</span>            stream.write(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">b'\xff'</span> * <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1024</span>)  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 增加音量</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">18</span>        <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">elif</span> current_volume > target_volume:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">19</span>            stream.write(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">b'\x00'</span> * <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1024</span>)  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 降低音量</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">20</span>    stream.close()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">21</span>    pa.terminate()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">22</span>    adjustment_status = <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">True</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">23</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">24</span>adjust_volume(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"work"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    用于调整此处的音量的算法相对简单。如果目标体积与当前体积不同,则调整速度可能会慢。

    提示:优化调整算法,例如根据间隙的大小动态调整调整步长的大小,可以使音量变化更快,更光滑。调整后,聆听声音并确认它是否满足期望。

    锻炼问题:将目标量设置为与当前音量非常不同,运行代码以查看调整效果;然后修改调整算法,使用动态步长大小,重新安置代码,观察更改并熟悉不同的操作。

    设置定时任务以自动切换场景卷:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">1</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">import</span> time<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">2</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">3</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">while</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">True</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">4</span>    current_time = time.localtime()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">5</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">if</span> current_time.tm_hour >= <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">9</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">and</span> current_time.tm_hour < <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">18</span>:  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 假设工作时间</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">6</span>        adjust_volume(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"work"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">7</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">elif</span> current_time.tm_hour >= <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">18</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">and</span> current_time.tm_hour < <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">22</span>:  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 假设娱乐时间</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">8</span>        adjust_volume(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"entertainment"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">9</span>    time.sleep(<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">60</span>)  <span style="font-size: inherit;color: rgb(0, 230, 118);line-height: inherit;"># 每分钟检查一次时间,调整音量</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    如果计算机时间不准确,场景判断将是错误的。

    https://img1.baidu.com/it/u=1408895556,2671705106&fm=253&fmt=JPEG&app=138&f=JPEG?w=500&h=1070

    提示:确保计算机时间是准确的,并且可以通过网络同步时间。设置正时任务时,间隔不应太短,以免频繁调整音量和占用系统资源。

    练习问题:故意错误地更改计算机时间,再次运行代码,然后查看调整卷的内容;然后将定时检查间隔更改为30秒,运行代码,查看计算机性能更改并熟悉不同的操作。

    处理可能的例外,例如音频设备故障:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">1</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">try</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">2</span>    pa = pyaudio.PyAudio()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">3</span>    stream = pa.open(format=pyaudio.paInt16,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">4</span>                     channels=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">5</span>                     rate=<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">44100</span>,<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">6</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">except</span> Exception <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">as</span> e:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">7</span>        print(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">f"音频设备打开出错: <span style="font-size: inherit;color: inherit;line-height: inherit;">{e}</span>"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    如果未处理异常,则脚本将在遇到问题时直接“放弃”它,并且随后的调整将被破坏。

    提示:例外处理应该是全面的。尝试根据不同的例外重新启动设备,更换驱动程序等;记录更多异常信息以促进根本原因。

    练习问题:模拟音频设备故障(例如禁用音频设备),重新运行代码以查看处理的例外;然后尝试完善异常处理,并为不同类型的异常(例如区分设备不存在并且设备占据问题)提供更详细的提示,请熟悉不同的操作。

    5。点亮“智能灯塔”- 可视化和调整结果的深入分析

    最后,我们必须点亮“智能灯塔”,以使自动计算机音量调整脚本更加完美。使用简单的文本输出来记录调整情况,例如总共调整了多少次,以及成功的次数:

<p style='margin-bottom:15px;color:#555555;font-size:15px;line-height:200%;text-indent:2em;'>    <pre style="font-size: 18px;color: rgb(62, 62, 62);line-height: inherit;letter-spacing: normal;text-align: start;background-color: rgb(255, 255, 255);"><code style="font-size: 9.5pt;color: rgb(255, 255, 255);line-height: 1.3;margin-right: 2px;margin-left: 2px;padding: 25px 0.5em 0.5em;border-radius: 8px;background: url("https://mmbiz.qpic.cn/mmbiz_svg/PFPUMLY8F76I0icsYJv8UqEBhEy2HUlh5dIiarEz3pu8E2OZxpsF4wxdeRW0AtTvNQTmlqghbIXQFWGZAWEOcpWJicOddRJ6UicZ/640?wx_fmt=svg&from=appmsg") 10px 10px / 40px no-repeat rgb(40, 44, 52);font-family: "courier new", Consolas, Inconsolata, monospace;display: block;overflow: auto hidden;border-width: 1px;border-style: solid;border-color: rgb(204, 204, 204); width: 1071px;overflow-wrap: normal !important;word-break: normal !important;"><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 1</span>success_count = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">0</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 2</span>total_count = <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">0</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 3</span><span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">while</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">True</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 4</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">try</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 5</span>        current_time = time.localtime()<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 6</span>        <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">if</span> current_time.tm_hour >= <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">9</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">and</span> current_time.tm_hour < <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">18</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 7</span>            adjust_volume(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"work"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 8</span>            success_count += <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;"> 9</span>            total_count += <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">10</span>        <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">elif</span> current_time.tm_hour >= <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">18</span> <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">and</span> current_time.tm_hour < <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">22</span>:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">11</span>            adjust_volume(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">"entertainment"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">12</span>            success_count += <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">13</span>            total_count += <span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">1</span><br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">14</span>        time.sleep(<span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;">60</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">15</span>    <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">except</span> Exception <span style="font-size: inherit;color: rgb(248, 35, 117);line-height: inherit;">as</span> e:<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">16</span>        print(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">f"调节出错: <span style="font-size: inherit;color: inherit;line-height: inherit;">{e}</span>"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/><span style="font-size: inherit;color: rgb(131, 135, 250);line-height: inherit;margin-right: 8px;padding-right: 8px;border-right: 1px solid yellowgreen;border-top-color: yellowgreen;border-bottom-color: yellowgreen;border-left-color: yellowgreen;display: inline-block;vertical-align: top;background-color: black;border-radius: 0px;">17</span>print(<span style="font-size: inherit;color: rgb(238, 220, 112);line-height: inherit;">f"本次共调节 <span style="font-size: inherit;color: inherit;line-height: inherit;">{total_count}</span> 次音量,成功 <span style="font-size: inherit;color: inherit;line-height: inherit;">{success_count}</span> 次"</span>)<br style="font-size: inherit;color: inherit;line-height: inherit;"/></code></pre></p>
    从此记录中,您可以直观地看到调整效果并提供优化的参考。例如,如果您发现调整在一定时间段内失败,则必须考虑场景判断是否错误并调整调整策略。

    提示:详细记录调整情况,留下更多的“线索”,将日志保存在文件中,然后长时间跟踪和调整质量。

    练习问题:将调整日志保存到文本文件中(使用Open()功能和文件写作操作,注意编码和其他问题),并熟悉不同的操作;然后在一段时间内分析日志,以查看是否有任何调整性调整问题的周期,请熟悉它们并优化进攻和防御。

    朋友,今天,我们学到了很多实际用途,可以自动调整计算机音量并根据场景设置音量大小,例如工作期间自动突变的技能,从了解计算机量控制的原理并持有工具到了解数据基础和实用构建。然后,为了加深可视化,每个步骤都在朝着舒适的计算机体验迈进。请记住,如果您有任何疑问,请练习更多动手,并随时在评论区域与我联系。祝您一切最好的学习和您的技能达到新的水平!
页: [1]
查看完整版本: Python音量控制教程:轻松变身电脑音量魔法师,实现自动调音