[英]Pygame.mixer.Sound .set_volume returns error in Raspberry Pi
我嘗試在Raspberry Pi B2中使用pygame創建一些簡短信號
這是我的代碼:
#!/usr/bin/python
import pygame
import time
from array import array
from pygame.locals import *
pygame.mixer.pre_init(44100, -16, 1, 1024)
pygame.init()
class ToneSound(pygame.mixer.Sound):
def __init__(self, frequency, volume):
self.frequency = frequency
pygame.mixer.Sound.__init__(self, self.build_samples())
self.set_volume(volume)
def build_samples(self):
period = int(round(pygame.mixer.get_init()[0] / self.frequency))
samples = array("h", [0] * period)
amplitude = 2 ** (abs(pygame.mixer.get_init()[1]) - 1) - 1
for time in xrange(period):
if time < period / 2:
samples[time] = amplitude
else:
samples[time] = -amplitude
return samples
tone_obj = ToneSound(frequency = 800, volume = .5)
tone_obj.play(-1) #the -1 means to loop the sound
time.sleep(2)
tone_obj.stop()
當我運行它時,我得到:
Traceback (most recent call last):
File "beep.py", line 29, in <module>
tone_obj = ToneSound(frequency = 800, volume = .5)
File "beep.py", line 15, in __init__
self.set_volume(volume)
TypeError: fromfile() takes exactly 2 arguments (1 given)
當我評論set_volume函數時,出現下一個錯誤:
Traceback (most recent call last):
File "beep.py", line 29, in <module>
tone_obj.play(-1) #the -1 means to loop the sound
TypeError: fromfile() takes exactly 2 arguments (1 given)
可能是什么問題?
我更新了pygame,更新了所有需要的庫-相同的結果。
根據pygame的文檔,set_volume只接受一個參數。
我真的不知道該怎么辦...
有一個類似的問題發布到Raspberry Pi網站,請參閱“摩爾斯電碼項目無法正常工作...” 。
那里的解決方案是:
pygame.mixer.Sound.init(self, self.build_samples())
pygame.mixer.Sound.init(self, buffer=self.build_samples())
添加buffer=
對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.