In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "how to use Python to implement a simple audio player". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Python libraries needed
pygame
tkinter
2. Easy UI Design audio_player = Tk()audio_player.title('Audio Player v1.0')audio_player.geometry('100x100+570+200')audio_player.maxsize(height=110, width=220)audio_player.minsize(height=110, width=220)3. Function module implementation 3.1 Select audio file to play def selectFile(): file = filedialog.askopenfile(mode='r', filetypes=[('AudioFile', '*.mp3')]) global filePath filePath = str(file).split("'")[1] try: playAudio() except: pass3.2 Control audio playback, pause def changeText(text): if text == 'play': return 'pause' if text == 'pause': return 'play'def playStop(): playBtn.config(text=changeText(playBtn.config('text')[4])) if playBtn.config('text')[4] == 'pause': mixer.music.unpause() else: if playBtn.config('text')[4] == 'play': mixer.music.pause()3.3 Control audio volume
Here we can define a global variable x, initialized to the value 0.5.
def audioINC(y): mixer.music.set_volume(y + 0.1) global x x += 0.1def audioDEC(y): mixer.music.set_volume(y - 0.1) global x x -= 0.13.4 Player initialization etc. def playAudio(): try: mixer.init() mixer.music.load(filePath) mixer.music.set_volume(x) playBtn.config(text='pause') mixer.music.play() except: pass4. Run frame = Frame(app)frame.place(x=35, y=20)openBtn = Button(frame, text='OpenFile', command=selectFile, width=8).grid(row=0, column=1)audioDec = Button(frame, text ='', command=lambda: audioDEC(x)).grid(row=1, column=0)playBtn = Button(frame, text='... ', command=playStop, width=8)playBtn.grid(row=1, column=1)audioInc = Button(frame, text='➕', command=lambda: audioINC(x)).grid(row=1, column=2)restartBtn = Button(frame, text='Restart', command=playAudio, width=8).grid(row=2, column=1)app.mainloop()5. Simple audio player demo
① Click the "Open File" button to open the local audio file
②"" and "" control the decrease and increase of volume respectively
③ Click the "Restart" button to replay the currently selected audio.
"How to use Python to achieve a simple audio player" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.