Python使用Pygame.mixer播放音乐
Python使用Pygame.mixer播放音乐
frequency这里是调频率...
播放网络中的音频:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author:Tea import pygame
import urllib2 pygame.mixer.init(frequency=8000,size=-16,channels=4)
URL = ("http://api.microsofttranslator.com/V2/Http.svc/Speak?language=zh-chs&appid=Tz49xk_OIwzAAeH91ExsdbHBruQdwsi5C2ssKhwhttRerOg__1cm_J-fxFJXAIME2&text=%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e6%a0%b7%e5%ad%90%ef%bc%8c%e4%bd%a0%e5%a5%bd%e5%a5%bd&format=audio/wav&options=MaxQuality")
response = urllib2.urlopen(URL)
waveFile = response.read()
pygame.mixer.Sound(waveFile).play()
while pygame.mixer.get_busy():
print ('playing...')
这玩意失真很大
播放本地的WAV
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author:Tea import pygame
pygame.mixer.init(frequency=15500,size=-16,channels=4)
waveFile = 'D:\C.wav'
pygame.mixer.Sound(waveFile).play()
while pygame.mixer.get_busy():
print ('playing...')
调调频率,还原度不错,能接受。。
pygame.mixer.init()
Initialize the mixer module for Sound loading and playback. The default arguments can be overridden to provide specific audio mixing. Keyword arguments are accepted. For backward compatibility where an argument is set zero the default value is used (possible changed by a pre_init call).
The size argument represents how many bits are used for each audio sample. If the value is negative then signed sample values will be used. Positive values mean unsigned audio samples will be used. An invalid value raises an exception.
The channels argument is used to specify whether to use mono or stereo. 1 for mono and 2 for stereo. No other values are supported (negative values are treated as 1, values greater than 2 as 2).
The buffer argument controls the number of internal samples used in the sound mixer. The default value should work for most cases. It can be lowered to reduce latency, but sound dropout may occur. It can be raised to larger values to ensure playback never skips, but it will impose latency on sound playback. The buffer size must be a power of two (if not it is rounded up to the next nearest power of 2).
Some platforms require the pygame.mixerpygame module for loading and playing sounds module to be initialized after the display modules have initialized. The top level pygame.init() takes care of this automatically, but cannot pass any arguments to the mixer init. To solve this, mixer has a function pygame.mixer.pre_init() to set the proper defaults before the toplevel init is used.
It is safe to call this more than once, but after the mixer is initialized you cannot change the playback arguments without first calling pygame.mixer.quit().
Python使用Pygame.mixer播放音乐的更多相关文章
- pygame 简单播放音乐程序
环境: python2.7 pygame 功能: 播放指定目录下的歌曲(暂时mp3),可以上一曲.下一曲播放. 文件目录: font 字体文件夹 image 图片文件夹 music 音乐文件夹 ...
- Python 用pygame模块播放MP3
安装pygame(这个是python3,32位的) pip安装这个whl文件 装完就直接跑代码啦,很短的 import time import pygame file=r'C:\Users\chan\ ...
- python3用pygame实现播放音乐文件
import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...
- python播放音乐
最近一直想实现使用Python播放音乐的功能,找了百度上的好多博客,要不就只能播放wav格式的,要不播放mp3格式的但无法在Linux系统下使用的,或者只能在Python2的情况下播放的,写的都不符合 ...
- 使用python播放音乐
1.首先安装pygame,pip install pygame 2.上代码: import time import pygame #音乐路径 filepath=r"C:\Users\1473 ...
- 吴裕雄--天生自然python学习笔记:python 用pygame模块处理音频文件
除了对图片. Word 等普通格式的文件进行处理外, Python 还有强大的多媒体文件操作能力,如对音频.视频 文件的操作 . 如果要播放音乐,我们可以用 pygame 包中的 mixer 对 象. ...
- Python音频操作+同时播放两个音频
对于python而言,音频的操作可以使用pygame包中的sound 和 music对象,本博客主要讲解这两个对象. 1.sound对象 Sound对象适合处理较短的音乐,如OGG和WAV格式的音频文 ...
- pygame “音乐盒”---- 播放一首歌& 点击对话框后背景以及对话框大小改变
有时,你用pygame写的游戏也许需要播放一些背景音乐,该怎么做呢,直接上代码: 下面的代码,有关于: 1>设置对话框图标.大小.标题 2>播放音乐 3>设置背景图片,以及获取背景图 ...
- 教你如何用python和pygame制作一个简单的贪食蛇游戏,可自定义
1.效果图 2.完整的代码 #第1步:导出模块 import pygame, sys, random from pygame.locals import * # 第2步:定义颜色变量,在pygame中 ...
随机推荐
- sqlserver使用OpenQuery或OPENROWSET遇到的问题
OpenQuery,OPENROWSET允许用户在链接服务器上查询.通过这种方法来得到查询的结果集. 1.在创建存储过程中,必须设置 SET ANSI_NULLS ...
- 作业.把c语言输出的基础差不多都概括了!
// (1)1英里=1.60931公里,从键盘上输入英里数输出公里数 #include "stdio.h" #define PI 3.1415926 #include " ...
- Hadoop与HBase中遇到的问题
1. Hadoop中遇到的问题 曾经所遇到的问题因为没有记录,所以忘了 (1)NameNode没有启动成功, 是因为你对HDFS多次格式化,导致datanode中与namenode中的VERSION文 ...
- Ubuntu server下安装JDK和Tomcat7
服务器是Ubuntu server 12.04 LTS 64bit 所有操作假设已经有root权限,若没有需要添加sudo. 一. 安装JDK 1.去Oracle官网下载jdk-6u45-linux- ...
- pat 1060. Are They Equal (25)
题目意思直接,要求将两个数转为科学计数法表示,然后比较是否相同 不过有精度要求 /* test 6 3 0.00 00.00 test 3 3 0.1 0.001 0.001=0.1*10^-2 p ...
- Java中数组的初始化方式
Java中数组的初始化方式 初始化方式有两种: 1.静态初始化:初始化时由程序猿显式指定每一个数组元素的初始值,由系统指定数组长度 2.动态初始化:初始化时由程序猿仅仅指定数组长度,由系统为数组 ...
- [转] 【开源访谈】Muduo 作者陈硕访谈实录
关于开源访谈 开源访谈是开源中国推出的一系列针对国内优秀开源软件作者的访谈,以文字的方式记录并传播.我们希望开源访谈能全面的展现国内开源软件.开源软件作者的现状,着实推动国内开源软件的应用与发展. [ ...
- [转] shared_from_this 几个值得注意的地方
http://hi.baidu.com/cpuramdisk/item/7c2f8d77385e0f29d7a89cf0 shared_from_this()是enable_shared_from_t ...
- BeautifulSoup在Windows下安装(running 2to3)
在windows下安装beautifulsoup经常会出现说是在python3下无法运行Python2的代码,这时需要将Python下的tool下的scripts目录添加到环境变量中,然后运行2to3 ...
- firebug中console命令尝试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...