(For beginners, the birds bypass 0.0)
Python version: 3.5.2
The source code is as follows:
__Author__ = "Lance#"# -*- coding = utf-8 -*-#导入相应模块from pygame import mixerfrom pynput import keyboardfrom pynput.keyboard import Key#音量初始值(范围是 0~1 )value = 0.5#混音器初始化、加载音乐、播放音乐mixer.init()mixer.music.load("xxx.mp3")mixer.music.play()#设置初始音量mixer.music.set_volume(value)def on_press(key): global value if key == Key.left: #暂停 mixer.music.pause() elif key == Key.right: #恢复暂停 mixer.music.unpause() elif key == Key.up: if value < 1: value += 0.1 mixer.music.set_volume(value) elif key == Key.down: if value > 0: value -= 0.1 mixer.music.set_volume(value)#开始监听键盘的动作with keyboard.Listener(on_press=on_press) as listener: listener.join()while True: pass
You can control the volume of the song, pause or not, by the left and right arrow keys.
If your environment does not have a module installed by the blogger, you can go to the command line and get the installation through the PIP Install Module name command.
I hope you enjoy your stay ^_^
A simple and fun python music player