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,就可以了.
随机推荐
- SQL Server中的完全连接(full join)
一.建库和建表 create database scort use scort create table emp ( empno int primary key, ename ), sal int, ...
- excel 公式 insert 语句
="insert into tb_fdn_deviceaccount (zdmc,czmc,sbbh,sbmc,SZCS,SBFLMC,SBLXMC,SBGG,SBYZ,SBJZ,SBXH, ...
- word公式大小
下面给出MathType与Word对应的字体关系,大家可以根据自己的实际需求,调整自己的公式大小. MathType与Word对应的字体关系示例 以上给大家详细介绍了调整MathType公式字体大小 ...
- Announcing Windows Template Studio in UWP
今天,我们很高兴地宣布您的文件→新的通用Windows平台应用程序在Visual Studio - Windows模板工作室中的下一个演变.Windows Template Studio在开发人员调查 ...
- Spring 使用介绍(八)—— 零配置(一)
一.概述 所谓零配置,并不是说一点配置都没有了,而是配置很少而已.通过约定来减少需要配置的数量,提高开发效率. 零配置实现主要有以下两种方式: 惯例优先原则:也称为约定大于配置(convention ...
- 错误代码 0x800700b7 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节
如果是运行VS时就报错,改个端口号就可以解决问题,改完以下两个地方重新运行
- docker--Dockerfile-nginx
# 基础镜像 FROM alpine # 作者信息 MAINTAINER NGINX Docker Maintainers "1024331014@qq.com" # 修改源 RU ...
- AtcoderARC062F Painting Graphs with AtCoDeer 【双连通分量】【polya原理】
题目分析: 如果一个双连通分量是简单环,那么用polya原理计数循环移位即可. 如果一个双连通分量不是简单环,那么它必然可以两两互换,不信你可以证明一下相邻的可以互换. 如果一条边是桥,那么直接乘以k ...
- 进入Docker容器的4种方式
进入Docker容器的4种方式 在使用Docker创建了容器之后,大家比较关心的就是如何进入该容器了,其实进入Docker容器有好几多种方式,这里我们就讲一下常用的几种进入Docker容器的方法. 进 ...
- Pleasant sheep and big big wolf HDU - 3046(最小割)
Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...