Jump to content

Hi all,

My question may or may not be very simple.

 

I posted this-

hoping that I would be able to find a program that, when I open Spotify, will automatically set the volume of all other programs to 50% the volume that my computer's set as.T

 

I had no real luck there, so I decided, with my very limited knowledge, to try and code something myself with Python. I'm using Python because we had to learn it for my maths class at school. I've done a few dumb things, like a meme questionnaire and a roulette board type of thing, but nothing using other applications or stuff of that sort.

 

I've coded my "program" to automatically open Spotify, and it has the code to be able to tell itself to close;

import subprocess

subprocess.Popen(['C:\Users\Tyson\AppData\Roaming\Spotify\Spotify.exe'])

ans = input("Say 'DONE' when you are.")
if ans == "DONE":
    print("Nice")

The last line is just a placeholder to make sure stuff would work haha, ignore that.

Also, it won't open Spotify itself. I've tried with other programs like Steam and it works perfectly, but with Spotify, it gives me this error-

https://prnt.sc/j8ktem

 

Basically, what I want is to be able to have the program to be able to take the computer's volume and halve it for all programs other than Spotify. e.g. If my computer's set at 20, have everything but Spotify set at 10, and also if someone could help me solve the error I'm getting that would be awesome.

And tell me if I'm doing stupid stuff that could be done easier and better with the code I've done, because I'm still learning Python, hoping to learn C++, probably after I graduate Year 12 and have the time.

 

Thank you all so much.

Link to comment
https://linustechtips.com/topic/919310-setting-volume-using-python/
Share on other sites

Link to post
Share on other sites

You can do this with pycaw; first, install pycaw from cmd or powershell:

pip install https://github.com/AndreMiras/pycaw/archive/master.zip

one of the examples seems to do exactly what you want:

"""
Mutes the volume of all processes, but unmutes chrome.exe process.
"""
from pycaw.pycaw import AudioUtilities


def main():
    sessions = AudioUtilities.GetAllSessions()
    for session in sessions:
        volume = session.SimpleAudioVolume
        if session.Process and session.Process.name() == "chrome.exe":
            volume.SetMute(0, None)
        else:
            volume.SetMute(1, None)


if __name__ == "__main__":
main()

just substitute "chrome.exe" with "Spotify.exe" and it should work.

 

As for opening spotify within the script, try using \\ instead of \ - \ is used as an escape character, which could cause problems, whereas \\ is the escape character for "\"

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×