Raspberry Pi 树莓派安装 pyaudio

使用树莓派安装 pyaudio 播放 wav 时交叉编译出错,使用 pip 安装步骤如下

  • 运行环境

    • Python 3.7.3
    • Raspbian buster
  • 安装步骤

    1. 安装 portaudio.dev
      1
      sudo apt install portaudio.dev
    2. 安装依赖库
      1
      sudo apt install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
    3. 安装 pyaudio
      1
      sudo pip3 install pyaudio

这时交叉编译已经通过说明已经成功安装了 pyaudio,但可能依旧无法播放 wav,继续执行一下步骤

安装 pulseaudio

1
2
sudo apt install pulseaudio
pulseaudio --start

此时运行即可播放声音

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pyaudio
from io import BytesIO


def player_wav(wav_bytes):
chunk = 1024
f = wave.open(BytesIO(wav_bytes), "rb")
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(f.getsampwidth()),
channels=f.getnchannels(),
rate=f.getframerate(),
output=True)
data = f.readframes(chunk)
while data != b'':
stream.write(data)
data = f.readframes(chunk)
stream.stop_stream()
stream.close()
p.terminate()


All articles in this blog adopt the CC BY-SA 4.0 agreement unless otherwise stated. Please indicate the source for reprinting!