Skip to content

example code for mp3 streaming #2845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions CircuitPython_MP3StreamPlayer/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# SPDX-FileCopyrightText: 2024 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Stream MP3 audio to I2S decoder
#
# Tested with:
#
# * Adafruit Metro ESP32-S3
# * Adafruit Metro ESP32-S2
# * Adafruit Feather ESP32 V2

import time

import adafruit_connection_manager
import adafruit_requests
import audiobusio
import audiomp3
import board
import wifi

mp3_buffer = bytearray(16384)
mp3_decoder = audiomp3.MP3Decoder("/silence.mp3", mp3_buffer)

pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)

STREAMING_URL = "https://ice2.somafm.com/dronezone-128-mp3"

if "D27" in dir(board):
# Feather ESP32 V2 has D27 instead of D11
i2s = audiobusio.I2SOut(bit_clock=board.D12, word_select=board.D13, data=board.D27)
else:
i2s = audiobusio.I2SOut(bit_clock=board.D12, word_select=board.D13, data=board.D11)

with requests.get(STREAMING_URL, headers={"connection": "close"}, stream=True) as response:
mp3_decoder.file = response.socket
i2s.play(mp3_decoder)
while i2s.playing:
time.sleep(0.1)
Binary file added CircuitPython_MP3StreamPlayer/silence.mp3
Binary file not shown.