Python加载声音
对于音频的操作可以使用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加载声音的更多相关文章
- Selenium3+python 加载Firefox配置
有小伙伴在用脚本启动浏览器时候发现原来下载的插件不见了,无法用firebug在打开的页面上继续定位页面元素,调试起来不方便 . 加载浏览器配置,需要用FirefoxProfile(profile_di ...
- as3.0 当fla里面有TLF文本的时候,加载声音会出现错误
问题描述 1.现有制作好的mp3加载包,这个包是相对路径 2.如果fla里面没有TLF文本,可以正常运行 解题思路 1.音频的相对路径和加载TLF文本的路径不一样,fla会优先选择TLF文件,这样mp ...
- python加载csv数据
入门机器学习时,一些测试数据是网络上的csv文件.这里总结了两种加载csv文件的方式: 1 通过numpy.urllib2加载 import numpy as np import urllib2 ur ...
- Python 加载mnist、cifar数据
import tensorflow.examples.tutorials.mnist.input_data mnist = input_data.read_data_sets("MNIST_ ...
- python加载json文件
主要是加载进来,之后就没难度了 import json path = 'predict2.json' file = open(path, "rb") fileJson = json ...
- python加载sqlite3报错:No module named _sqlite3
环境为Ubuntu16.04 Apache2.4 Python2.7.13 django 1.8 今天部署apache+django,经过各种折腾,好不容易配置完了,发现错误Apache的日志里有一项 ...
- vs2015利用python加载dll调试配置
python调用dll相对而言比较方便,写个脚本调试轻松工作,快乐生活. python脚本 from ctypes import * import time # 脚本挂起 input() # load ...
- python加载和使用java的类的方法
在开发python项目的时候,有时候会用的java的jar包 有这么几个python的三方包可以用: pyjnius:bug list:https://github.com/kivy/pyjnius/ ...
- python加载不了cookirlib模块的问题
Python 3 改成 http.cookiejar了,所以import cookielib只要改成import http.cookiejar,就可以了.
随机推荐
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0'
七月 05, 2018 10:26:54 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRul ...
- Js--动态生成表格
<div> <h1>动态生成表格</h1> <div id="table1"> 行 ...
- LODOP打印控件进行批量打印
Lodop打印控件批量打印的方式:1.批量打印每页内容相同的:(1)批量打印相同内容的很多纸张,可以设置打印份数,把该内容打印出多份.2.批量打印每页不同内容的:(1)通过在一个任务中分页,循环添加页 ...
- poj-2752(拓展kmp)
题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...
- 洛谷P1781宇宙总统题解
题目 此题原本是一个简单的排序,但因为数据范围的限制,所以变成了一个需采用字符串排序的题目,接下来我将给大家讲一下如何字符串排序. 首先先判断为位数上是否相同,如果不同再比较那一位的数就可以了 #in ...
- [BJWC2010] 严格次小生成树
[BJWC2010]严格次小生成树算法及模板 所谓次小生成树,即边权之和第二小的生成树,但所谓严格,就是不能和最小的那个相等. 求解严格次小生成树的方法一般有倍增和LCT两种.当然LCT那么高级的我当 ...
- Power Stations HDU - 3663
我为什么T了.... Power Stations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- 【XSY1905】【XSY2761】新访问计划 二分 树型DP
题目描述 给你一棵树,你要从\(1\)号点出发,经过这棵树的每条边至少一次,最后回到\(1\)号点,经过一条边要花费\(w_i\)的时间. 你还可以乘车,从一个点取另一个点,需要花费\(c\)的时间. ...
- 登录Linux服务器显示IP和自定义备注
默认搭建好的Linux服务器,使用Xshell登录的窗口如下所示: 可根据需要执行如上代码,再重新登录服务器,效果如下图所示: 代码片段:echo "export PS1='\u@\[\e[ ...
- angularJS 路由加载js controller 未定义 解决方案
说明 本文主要说明,在angularJS框架使用中,angularJS 路由加载js controller 未定义 解决方案. 路由 $routeProvider 异步加载js 路由的基本用法,请查看 ...