【问题标题】:No sound from audio cdrom with PyGame on RaspBerry Pi 4+, using ALSA使用 ALSA 在 RaspBerry Pi 4+ 上使用 PyGame 的音频 cdrom 没有声音
【发布时间】:2020-08-28 17:49:36
【问题描述】:

我尝试使用 PyGame 在带有 python3 的 Raspberry Pi4 上播放旧的音频 cd 收藏。 据我所知,cd audio 的数据结构与声音或流媒体不同,并且音频 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。 我不知道我犯了什么错误。希望这里的其他人能给我一个建议。

【问题讨论】:

  • 你有 CDROM 连接到树莓派吗?
  • 是的,它是一个没有音频输出的外部 USB CDROM。正如我所描述的,VLC 播放音轨并通过 Raspberry Pi 的模拟输出向我的外部 Bose SoundTouch 10 发出声音。PyGame 在我看来,它向 CDROM 发送命令(我可以听到音轨的变化和工作CDROM 的声音),但它不会向外部扬声器发送音乐。没有音乐,没有乐趣!!!
  • 我没有 CDROM 来连接 RPi 并看到这个问题。我什至不能在我的笔记本上查看它,因为我几年没有 CDROM。坦率地说,我很惊讶有人还在使用 CDROM。
  • 我的问题的解决方法非常非常非常简单!!!

标签: python-3.x audio raspberry-pi pygame alsa


【解决方案1】:

PyGame 仅控制音频 CD(播放、暂停、停止等)。音乐本身必须取自 CDROM 的音频输出!较旧的设备确实有这样的输出,但我从未见过具有这种音频输出的新 USB 驱动器。所以我不能在我的项目中使用 PyGame。希望我的错能帮助其他人不要落入同样的陷阱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2016-12-01
    • 2015-04-08
    相关资源
    最近更新 更多