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
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().

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM