How to change volume with Python?

被刻印的时光 ゝ 提交于 2019-12-07 18:23:11

问题


I'm writing a program to wake me up mornings, but I want my program to play alarm sound as loud as its possible. so it needs to raise up volume to 100%. but I don't know how. I'm using python3 on macOS Sierra.


回答1:


You can control the volume of your computer with Applescript:

set volume output volume 100

To execute Applescript from python you can use py-applescript which can be installed with sudo easy_install py-applescript. The following script will set the volume:

import applescript

applescript.AppleScript("set volume output volume 100").run()

EDIT: For Python3.6 you can use osascript instead: pip3.6 install osascript and:

import osascript

osascript.osascript("set volume output volume 100")



回答2:


You do not need anything outside of the standard library to do this in python. Apple supports executing AppleScript from the terminal so the subprocess module is sufficient.

from subprocess import call
call(["osascript -e 'set volume output volume 100'"], shell=True)


来源:https://stackoverflow.com/questions/45772011/how-to-change-volume-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!