Python使用Pygame.mixer播放音乐

frequency这里是调频率...

播放网络中的音频:

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #Author:Tea
  4.  
  5. import pygame
  6. import urllib2
  7.  
  8. pygame.mixer.init(frequency=8000,size=-16,channels=4)
  9. 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")
  10. response = urllib2.urlopen(URL)
  11. waveFile = response.read()
  12. pygame.mixer.Sound(waveFile).play()
  13. while pygame.mixer.get_busy():
  14. print ('playing...')

这玩意失真很大

播放本地的WAV

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #Author:Tea
  4.  
  5. import pygame
  6. pygame.mixer.init(frequency=15500,size=-16,channels=4)
  7. waveFile = 'D:\C.wav'
  8. pygame.mixer.Sound(waveFile).play()
  9. while pygame.mixer.get_busy():
  10. print ('playing...')

调调频率,还原度不错,能接受。。

pygame.mixer.init()

initialize the mixer module
init(frequency=22050, size=-16, channels=2, buffer=4096) -> None

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播放音乐的更多相关文章

  1. pygame 简单播放音乐程序

    环境: python2.7 pygame 功能: 播放指定目录下的歌曲(暂时mp3),可以上一曲.下一曲播放. 文件目录: font  字体文件夹 image  图片文件夹 music  音乐文件夹 ...

  2. Python 用pygame模块播放MP3

    安装pygame(这个是python3,32位的) pip安装这个whl文件 装完就直接跑代码啦,很短的 import time import pygame file=r'C:\Users\chan\ ...

  3. python3用pygame实现播放音乐文件

    import pygameimport time #导入音乐文件file = r'C:\1.wav'pygame.mixer.init()track = pygame.mixer.music.load ...

  4. python播放音乐

    最近一直想实现使用Python播放音乐的功能,找了百度上的好多博客,要不就只能播放wav格式的,要不播放mp3格式的但无法在Linux系统下使用的,或者只能在Python2的情况下播放的,写的都不符合 ...

  5. 使用python播放音乐

    1.首先安装pygame,pip install pygame 2.上代码: import time import pygame #音乐路径 filepath=r"C:\Users\1473 ...

  6. 吴裕雄--天生自然python学习笔记:python 用pygame模块处理音频文件

    除了对图片. Word 等普通格式的文件进行处理外, Python 还有强大的多媒体文件操作能力,如对音频.视频 文件的操作 . 如果要播放音乐,我们可以用 pygame 包中的 mixer 对 象. ...

  7. Python音频操作+同时播放两个音频

    对于python而言,音频的操作可以使用pygame包中的sound 和 music对象,本博客主要讲解这两个对象. 1.sound对象 Sound对象适合处理较短的音乐,如OGG和WAV格式的音频文 ...

  8. pygame “音乐盒”---- 播放一首歌& 点击对话框后背景以及对话框大小改变

    有时,你用pygame写的游戏也许需要播放一些背景音乐,该怎么做呢,直接上代码: 下面的代码,有关于: 1>设置对话框图标.大小.标题 2>播放音乐 3>设置背景图片,以及获取背景图 ...

  9. 教你如何用python和pygame制作一个简单的贪食蛇游戏,可自定义

    1.效果图 2.完整的代码 #第1步:导出模块 import pygame, sys, random from pygame.locals import * # 第2步:定义颜色变量,在pygame中 ...

随机推荐

  1. Tomcat Server Locations

  2. mybatis处理单表多表操作以及动态sql和批量操作

    一.单表操作 建立了相应的mapper映射接口类后,在src/main/resources目录下的自己建立的mapper文件夹下创建映射类 select操作:用select标签 属性id为映射接口的方 ...

  3. 谁动了我的timer?——C#的垃圾回收和调试

    先来看如下的一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 using System; using System.Threading; publi ...

  4. iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)

    iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...

  5. Jinja2模版语言自定义filter的使用

    Jinja2模版语言,自带有一些filter,能够在前端的模版中控制数据按照相应的方式显示.比如以下两种filter,分别能在前端控制数字的近似精度显示和根据字符串长度补齐: round(value, ...

  6. Cocos2d-X学习——Android移植,使用第三方库.so被删掉问题

    2014-05-26 导语:Cocos2dx在安卓上移植的时候,增加第三方库,却发现新加的so库被删掉了. 正文: 1.我的环境: cocos2d-x 2.2.3, ndk-r9 2.网上找了非常多, ...

  7. 多版本号并发控制(MVCC)在分布式系统中的应用

    QQ群:289150599 问题 近期项目中遇到了一个分布式系统的并发控制问题.该问题能够抽象为:某分布式系统由一个数据中心D和若干业务处理中心L1,L2 ... Ln组成:D本质上是一个key-va ...

  8. 一种解决的方法:CGContextSaveGState: invalid context 0x0

    遇到这个问题找了好久答案,最后排错排出来了 CGContextSaveGState: invalid context 0x0. This is a serious error. This applic ...

  9. Android 模仿微信启动动画(转)

    本文内容 环境 项目结构 演示微信启动动画 本文演示微信启动动画.请点击此处下载,自行调试. 顺便抱怨一下,实践性(与研究性质的相对)技术博的“七宗罪”: 第一宗罪,错字连篇,逻辑不清: 第二宗罪,文 ...

  10. java多态---ABC案列

    class A{ public void show(){ show2(); } public void show2(){ System.out.println("我"); } } ...