如何利用Python实现简易的音频播放器

作者:LeBron?Le 时间:2022-07-16 11:47:32 

1. 需要用到的Python库

  • pygame

  • tkinter

2. 简易UI设计

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. 功能模块实现

3.1 选择音频文件进行播放

def selectFile():
    file = filedialog.askopenfile(mode='r', filetypes=[('AudioFile', '*.mp3')])
    global filePath
    filePath = str(file).split("'")[1]
    try:
        playAudio()
    except:
        pass

3.2 控制音频播放、暂停

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 控制音频音量大小

这里可以定义一个全局变量x,初始化为值0.5。

def audioINC(y):
    mixer.music.set_volume(y + 0.1)
    global x
    x += 0.1

def audioDEC(y):
    mixer.music.set_volume(y - 0.1)
    global x
    x -= 0.1

3.4 播放器初始化等细节

def playAudio():
    try:
        mixer.init()
        mixer.music.load(filePath)
        mixer.music.set_volume(x)
        playBtn.config(text='pause')
        mixer.music.play()
    except:
        pass

4. 运行

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. 简易音频播放器展示图

如何利用Python实现简易的音频播放器

  • ①点击“OpenFile”按钮可以打开本地音频文件

  • ②“➖”和“➕”分别控制音量的减小和增大

  • ③点击"Restart"按钮可以重新播放当前选中的音频

6. 总结

本文仅仅是实现了一个简易的音频播放器,UI极其简陋,为了仅仅是实现音频播放的功能,仅供学习参考。

来源:https://blog.csdn.net/hutianle/article/details/123204633

标签:Python,音频,播放器
0
投稿

猜你喜欢

  • Python使用enumerate获取迭代元素下标

    2023-03-05 01:46:31
  • DOM_window对象属性之--clipboardData对象操作代码

    2011-02-05 10:49:00
  • cookies应对python反爬虫知识点详解

    2023-12-16 06:15:18
  • Oracle回滚段的概念,用法和规划及问题的解决

    2010-07-26 13:08:00
  • 了解不常见但是实用的Python技巧

    2022-10-12 09:07:35
  • js游戏 俄罗斯方块 源代码

    2008-01-24 13:14:00
  • Mootools 1.2教程(18)——Class 类(第一部分)

    2008-12-19 12:45:00
  • PHP设计模式之模板方法模式Template Method Pattern详解

    2023-05-25 00:24:26
  • 有效防止ASP木马上传运行—小知识[网络安全技术]

    2011-03-06 11:15:00
  • asp 判断是否为搜索引擎蜘蛛的代码

    2011-03-10 11:03:00
  • 如何在服务器端调用winzip命令行对文件压缩和解压

    2008-01-26 20:44:00
  • python 脚本生成随机 字母 + 数字密码功能

    2021-11-27 23:37:10
  • CSS Hack经验总结

    2008-05-01 13:13:00
  • asp三天学好ADO对象之第二天

    2008-10-09 12:49:00
  • jQuery性能优化指南[译]

    2009-05-12 11:54:00
  • Dreamweaver MX新功能试用:连续空格

    2008-01-06 21:03:00
  • pandas 添加空列并赋空值案例

    2022-12-04 13:48:16
  • ajax返回中文乱码问题解决

    2009-04-13 16:07:00
  • python logging日志模块原理及操作解析

    2022-10-30 02:44:31
  • php去掉数组的第一个值的两个函数:array_shift、array_splice

    2023-05-29 02:26:07
  • asp之家 网络编程 m.aspxhome.com