对于音频的操作可以使用pygame包中的sound 和 music对象进行音乐播放。

Sound对象适合处理较短的音乐,如OGG和WAV格式的音频文件。

Music对象出来可以播放OGG、WAV音频外,还可以播放mp3格式。

from pygame import mixer   #mixer对象可以播放音乐

一、Sound 对象

mixer.init()  先初始化

创建Sound对象

sound = mixer.Sound(音频文件名)

sound.play(loops = 0)   播放音乐,loops代表播放次数,默认为0,表示播放一次;loops=5可播放6次;loops=-1 可重复播放

sound.stop()   结束播放

sound.set_volume(value)   设置音量,音量有最低到高0.0--1.0

sound.get_volume()  获取当前的音量

二、music对象

mixer.init()  先初始化

mixer.music.load(filename)   停止正在播放的音乐,filename为歌曲的文件名

mixer.music.play(loops=0,start=0.0)   播放歌曲,loops表示播放次数,默认为0,播放1次;loops=5可以播放6次;loop=-1可以重复播放

mixer.music.stop()  停止播放

mixer.music.pause()  暂停播放

mixer.music.unpause()   用pause()暂停后,必须使用这个函数来继续播放

mixer.music.set_volume(value)  设置音量,最大1.0,最小0.0

mixer.music.get_volume()  获取当前音量

mixer.music.get_busy()  检查歌曲播放状态,True为正在播,False为不在播

 from pygame import mixer
import glob
import tkinter as tk mixer.init()
win = tk.Tk()
win.geometry("640x380")
win.title("mp3 播放器") labeltitle = tk.Label(win,text='mp3 播放器',fg = 'red')
labeltitle.pack() framel = tk.Frame(win)
framel.pack() musicList = glob.glob('*.mp3') playSong = preplaySone = ''
index = 0
volume = 0.6
choice = tk.StringVar() def choose():
global playSong
msg.set("播放歌曲:"+choice.get())
playSong = choice.get() def playMp3():
global status,playSong,preplaySone
if playSong == preplaySone:
if not mixer.music.get_busy():
mixer.music.load(playSong)
mixer.music.play(loops = -1)
else:
mixer.music.pause()
else:
playNewSong()
preplaySone = playSong def playNewSong():
global playSong
mixer.music.stop()
mixer.music.load(playSong)
mixer.music.play(loops = -1)
msg.set("正在播放:{}".format(playSong)) def pauseMp3():
mixer.music.pause()
msg.set("暂停歌曲:{}".format(playSong)) def increase():
global volume
volume += 0.1
if volume >=1:
volume = 1
mixer.music.set_volume(volume) def decrease():
global volume
volume -= 0.1
if volume <= 0.1:
volume = 0.1
mixer.music.set_volume(volume) def stopMp3():
mixer.music.stop()
msg.set("\n停止播放") def exitMp3():
mixer.music.stop()
win.destroy() for music in musicList:
rbtem = tk.Radiobutton(framel,text=music,variable = choice,value=music,command=choose)
if index == 0:
rbtem.select()
playSong = preplaySone = music
rbtem.grid(row = index,column = 0,sticky = 'w')
index += 1 msg = tk.StringVar()
msg.set("\n播放歌曲:"+playSong)
label = tk.Label(win,textvariable=msg,fg='blue')
label.pack() labelsep = tk.Label(win,text='\n')
labelsep.pack() frame2 = tk.Frame(win)
frame2.pack() button1 = tk.Button(frame2,text='播放',width=8,command = playMp3)
button1.grid(row=0,column=0,padx=5,pady=5) button2 = tk.Button(frame2,text='暂停',width=8,command = pauseMp3)
button2.grid(row=0,column=1,padx=5,pady=5) button3 = tk.Button(frame2,text='音量调大',width=8,command = increase)
button3.grid(row=0,column=2,padx=5,pady=5) button4 = tk.Button(frame2,text='音量调小',width=8,command = decrease)
button4.grid(row=0,column=3,padx=5,pady=5) button5 = tk.Button(frame2,text='停止',width=8,command = stopMp3)
button5.grid(row=0,column=4,padx=5,pady=5) button6 = tk.Button(frame2,text='结束',width=8,command = exitMp3)
button6.grid(row=0,column=5,padx=5,pady=5) win.protocol("WM_DELETE_WINDOW",exitMp3)
win.mainloop()
#print(musicList)

Python加载声音的更多相关文章

  1. Selenium3+python 加载Firefox配置

    有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_di ...

  2. as3.0 当fla里面有TLF文本的时候,加载声音会出现错误

    问题描述 1.现有制作好的mp3加载包,这个包是相对路径 2.如果fla里面没有TLF文本,可以正常运行 解题思路 1.音频的相对路径和加载TLF文本的路径不一样,fla会优先选择TLF文件,这样mp ...

  3. python加载csv数据

    入门机器学习时,一些测试数据是网络上的csv文件.这里总结了两种加载csv文件的方式: 1 通过numpy.urllib2加载 import numpy as np import urllib2 ur ...

  4. Python 加载mnist、cifar数据

    import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read_data_sets("MNIST_ ...

  5. python加载json文件

    主要是加载进来,之后就没难度了 import json path = 'predict2.json' file = open(path, "rb") fileJson = json ...

  6. python加载sqlite3报错:No module named _sqlite3

    环境为Ubuntu16.04 Apache2.4 Python2.7.13 django 1.8 今天部署apache+django,经过各种折腾,好不容易配置完了,发现错误Apache的日志里有一项 ...

  7. vs2015利用python加载dll调试配置

    python调用dll相对而言比较方便,写个脚本调试轻松工作,快乐生活. python脚本 from ctypes import * import time # 脚本挂起 input() # load ...

  8. python加载和使用java的类的方法

    在开发python项目的时候,有时候会用的java的jar包 有这么几个python的三方包可以用: pyjnius:bug list:https://github.com/kivy/pyjnius/ ...

  9. python加载不了cookirlib模块的问题

    Python 3 改成 http.cookiejar了,所以import cookielib只要改成import http.cookiejar,就可以了.

随机推荐

  1. Windows 访问 CentOS 7 共享文件夹 Samba 配置

    Windows 使用用户名.密码访问 CentOS 7 共享文件夹 执行命令,查看 Windows 工作组:net config workstation 执行命令,安装 Samba:yum insta ...

  2. .NET Core 2.0及.NET Standard 2.0 Description

    NET Core 2.0的发布时间,.NET Core 2.0预览版及.NET Standard 2.0 Preview大概在5月中旬或下旬发布. .NET Core 2.0正式版本发布时间大约在Q3 ...

  3. 以@GetMapping为例,SpringMVC 组合注解

    @GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写.该注解将HTTP Get 映射到 特定的处理方法上.

  4. Linux(Centos)下调整分区大小(以home和根分区为例)

      在安装新系统的时候,有时候没法预估或者说错误的划分了分区大小,常常会导致我们后面的操作出现极大地不方便,比如某个分区分的太小了,导致 软件安装的时候会报安装空间不够,这就很麻烦.在这里我就记录一下 ...

  5. Quartus prime 16.0 signaltap II 使用

    前言 由于逻辑分析仪太贵,altera贴心提供signal tap II来观察输出波形,不过使能signaltap II会占用片内ram,毕竟原理就是把数据采样到ram中再通过jtag口上传到quar ...

  6. centos6.8下安装破解quartus prime16.0以及modelsim ae安装

    前言 装逼使用 流程 安装modelsim: 1.modelsim ae在linux下是32位的,对于64位系统需要安装32位库:yum install xulrunner.i686 2.给予权限: ...

  7. atcoder NIKKEI Programming Contest 2019 E - Weights on Vertices and Edges

    题目链接:Weights on Vertices and Edges 题目大意:有一个\(n\)个点\(m\)条边的无向图,点有点权,边有边权,问至少删去多少条边使得对于剩下的每一条边,它所在的联通块 ...

  8. 【XSY1602】安全网络 树形DP 数学

    题目大意 有一颗树,要为每个节点赋一个值\(l_i\leq a_i\leq r_i\),使得任意相邻的节点互素.然后对每个节点统计\(a_i\)在所有可能的情况中的和. \(n\leq 50,1\le ...

  9. MT【245】小概率事件

    (2011年AAA测试)将一枚均匀的硬币连续抛掷$n$次,以$P_n$ 表示未出现连续3次正面的概率.求$\{P_n\}$.并讨论$\{P_n\}$单调性和极限. 分类讨论:第$n$次反面则未出现连续 ...

  10. 【总结】字符串hash

    序列字符串\(Hash\) 直接hash即可qwq 预处理:\(Hash[3][i]\)(\(Hash\)值),\(Pow[3][i]\)(用来乘系数) 判断相等:\(box_1=Hash[3][i] ...