我尝试使用PyGame在带有python3的Raspberry Pi4上播放旧的音频CD集合。据我所知,cd音频与声音或流媒体具有不同的数据结构,并且音频CD没有像数据CD一样安装在文件系统中,因此需要特殊处理。下面的非常简单的程序可以在Windows 10下从Python的命令行运行,也可以在PyCharm IDE中运行:
PS C:\Users\manfr> python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pygame import cdrom
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> import pygame
>>> import time
>>> pygame.init()
(6, 0)
>>> cdrom.init()
>>> cdrom.CD(0).init()
>>>
>>> i = 0
>>> while i == 0:
... cdrom.CD(0).play(5)
... time.sleep(10)
... i = 1
... cdrom.CD(0).stop()
...
>>>
也在PyCharm中也没有问题:
from pygame import cdrom
import pygame
import time
pygame.init()
cdrom.init()
cdrom.CD(0).init()
i = 0
while i == 0:
cdrom.CD(0).play(5)
time.sleep(10)
i = 1
cdrom.CD(0).stop()
在PyCharms的“运行”窗口中显示结果:
D:\Python-Programme\gui-beispiel\venv\Scripts\python.exe "D:/Python-Programme/gui-beispiel/CD-Project/Thread Test 10 PyGame playlist vereinfacht.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Process finished with exit code 0```
And now the same code starting in the 'Python Console' of PyCharm.
在Windows 10环境中没有问题。RaspberryPi使它复杂一些。
我的RaspBerry Pi上的同一程序既没有从命令行(python 3.x),也没有在Thonny Python IDE下返回任何音乐。从python命令行解释器运行:
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> #!/usr/bin/python3
... import pygame
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> from pygame import *
>>> from pygame import cdrom
>>> import time
>>>
>>> pygame.init()
(6, 0)
>>> cdrom.init()
>>> cdrom.CD(0).init()
>>>
>>> i = 0
>>> while i == 0:
... print('cdrom bussy, but I hear no music')
... cdrom.CD(0).play(5)
... time.sleep(20)
... i = 1
... cdrom.CD(0).stop()
... print("no idea what's went wrong :-(")
...
cdrom bussy, but I hear no music
no idea what's went wrong :-(
>>>
以下命令都很好用:
pi@raspberrypi:~ $ speaker-test -t wav -c 2
speaker-test 1.1.8
Playback device is default
Stream parameters are 48000 Hz, S16_LE, 2 channels
WAV file (s)
Rate is 48000 Hz (requested: 48000 Hz)
Buffer size from 480 to 32768
Period size from 480 to 32768
Use maximum buffer size 32768
Periods = 4
set: period_size = 8192
set: buffer_size = 32768
0 - Front left
1 - Front right
pi@raspberrypi:~ $ omxplayer /home/pi/test.wav
Audio codec pcm_u8 channels 1 samplerate 11025 bitspersample 8
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
pi@raspberrypi:~ $ omxplayer /home/pi/song1.mp3
Audio codec mp3float channels 2 samplerate 44100 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)
在我看来,wav / mp3音频通道没有任何问题,但是我的raspi不喜欢来自“已安装”音频CD的音频轨道。我还对安装在raspi上的VLC播放器进行了测试。打开媒体(cdrom)并打开并行alsamixer
显示,vlc与ALSA成功通信。例如。我可以改变响度。这是ALSA的信息屏幕,可提供更多信息。
┌────────────────────────────── AlsaMixer v1.1.8 ──────────────────────────────┐
│ Gerät: bcm2835 ALSA F1: Hilfe │
│ Chip: Broadcom Mixer F2: System-Informationen │
│ Ansicht: F3:[Wiedergabe] F4: Aufnahme F5: Alle F6: Soundkarte auswählen │
│ Element: PCM [dB-Änderung: -9,58] Esc: Beenden │
│ ┌──┐ │
│ │ │ │
│ │▒▒│ │
│ │▒▒│ │
│ ├──┤ │
│ │OO│ │
│ └──┘ │
│ 59 │
│ < PCM > │
└──────────────────────────────────────────────────────────────────────────────
[到目前为止,我认为这不是我的raspi的声音配置问题,而是PyGame的问题。似乎PyGame无法与ALSA配合使用。我不知道我犯了什么错误。希望这里的其他任何人都能给我建议。
PyGame仅控制音频CD(播放,暂停,停止等)。音乐本身必须从CDROM的音频输出中获取!较旧的设备确实具有这样的输出,但是我从未见过具有这种音频输出的新USB驱动器。所以我不能在我的项目中使用PyGame。希望我的过失会帮助其他人不要陷入同样的陷阱。