Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

How can i play audio on a bluetooth speaker using python on a raspberry pi 4 model B

I have a script that is supposed to play sound when its triggered by an arduino,

#!/usr/bin/python3.7
import serial
from pydub import AudioSegment
from pydub.playback import play

Sound1 = AudioSegment.from_wav("/home/pi/Desktop/Refridgerator/Gamesound.wav")
Sound2 = AudioSegment.from_wav("/home/pi/Desktop/Refridgerator/Grenade.wav")

ser = serial.Serial('/dev/ttyACM0', 9600)
while 1:
    line = ser.readline().decode('utf-8').rstrip()
    if (line == "1"):
        play(Sound1)
    elif (line == "2"):
        play(Sound2)
    print(line)

I also have a systemd file to automatically start up that script when the raspberry pi is turned on,

[Unit] 
Description=The refridgerator thing 
After=multi-user.target 
[Service] 
Type=simple 
ExecStart=/usr/bin/python3 /home/pi/Refridgerator.py 
Restart=on-abort 
[Install] 
WantedBy=multi-user.target

My problem is, the script works as expected when connected to a HDMI monitor(the monitor has built in speakers), but when i try to use a Bluetooth speaker that's paired, connected, and trusted, the audio wont play through the speaker, can i have help! its in python 3.

question from:https://stackoverflow.com/questions/65908222/how-can-i-play-audio-on-a-bluetooth-speaker-using-python-on-a-raspberry-pi-4-mod

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...