<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[cardputer use built in speaker to play wav file error]]></title><description><![CDATA[<p dir="auto"><img src="/assets/uploads/files/1752374724671-e317c820-c7c3-4ff4-b386-d59a2e49e91e-image.png" alt="e317c820-c7c3-4ff4-b386-d59a2e49e91e-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">the init of the I2S speaker is failed<br />
I cannot find any demo on the web and in official doc, the demo is not as what I can find in UIflow, any one know how to use it ?</p>
]]></description><link>https://community.m5stack.com/topic/7679/cardputer-use-built-in-speaker-to-play-wav-file-error</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 20:13:52 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/7679.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 13 Jul 2025 02:47:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cardputer use built in speaker to play wav file error on Wed, 25 Feb 2026 10:55:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/zhouyousong" aria-label="Profile: zhouyousong">@<bdi>zhouyousong</bdi></a> For anyone else who is curious about how to get around this problem, the <code>playRaw()</code> method of <code>M5.Speaker</code> is non-blocking, but requires you to provide the PCM data  directly as a <code>bytearray</code> or similar, as well as the sample rate.</p>
<p dir="auto">If you look up the encoding of a .WAV file, it is fairly trivial to extract the sample rate (it's bytes 24:28 in the 44-byte header included at the start of any .Wav file). Getting the PCM data is then simply a case of reading the byte values from byte 44 onwards, until you reach the end of the file.</p>
<p dir="auto">Here's a basic sketch:</p>
<pre><code>fd = open('wavfile.wav', 'rb') # Open .wav file in read (bytes) mode

header = fd.read(44) # Read .wav file header
sample_rate = int.from_bytes(header[24:28], 'little') # Extract sample-rate
... # Can also extract other header information, such as bits per sample, number of channels, etc.

buffer_time = 1 # 1s worth of audio data
buf = bytearray(sample_rate*(bits_per_sample//8)*buffer_time) # Buffer large enough to contain 1s of audio

# Loop
fd.readinto(buf, sample_rate) # Read from file into buffer
Speaker.playRaw(buf, sample_rate) # Non-blocking playback of audio data in buffer
... # and repeat for each additional second of audio in .wav file
</code></pre>
<p dir="auto">This is a useful <a href="https://docs.fileformat.com/audio/wav/" target="_blank" rel="noopener noreferrer nofollow ugc">link</a> to understand the contents of .WAV file header.</p>
<p dir="auto">The only potentially tricky part in this is timing the file reads so they don't block the rest of your program for a long time.<br />
I found that reading 1 second of 8000 samples per second, 16-bit, mono audio from the SD card takes about 67ms for the M5 FIRE (v1.0). Using a smaller buffer (0.5s/0.25s of audio etc.) will mean correspondingly shorter read times, with the caveat that you will have to read more often.</p>
<p dir="auto">You can try the following code in the Block Designer: (note - it uses Timer 0 to time the periodic file reads, so if you are using Timers for other things, you will need to adjust accordingly).</p>
<pre><code>"""
file     NonBlockingSpeaker
time     2026-02-24
author   Tom Fahey
email   tomp.fahey@gmail.com
license  MIT
"""

from machine import Timer
from M5 import Speaker
import micropython
"""
file     NonBlockingSpeaker
time     2026-02-24
author   Tom Fahey
email   tomp.fahey@gmail.com
license  MIT License
"""

class NonBlockingSpeaker:
    """
    note:
        en: ''
    details:
        color: '#0fb1d2'
        link: https://github.com/m5stack
        image: ''
        category: Custom
    example: ''
    """




    def __init__(self):
        """
        label:
            en: '%1 init'
        """
        self.tim = Timer(0)

    def __setup(self, wav_file):
        """
        label:
            en: ' %1 setup, wav_file: %2'
        params:
            wav_file:
                name: wav_file
        """
        self.fd = open(wav_file, 'rb')
        self.header = self.fd.read(44)
        self.file_size = int.from_bytes(self.header[0:4], 'little')
        self.fmt_type = int.from_bytes(self.header[20:22], 'little')
        self.channels = int.from_bytes(self.header[22:24], 'little')
        self.sample_rate = int.from_bytes(self.header[24:28], 'little')
        self.bits_per_sample = int.from_bytes(self.header[34:36], 'little')
        self.data_size = int.from_bytes(self.header[40:44], 'little')
        self.buffer = bytearray(self.sample_rate*(self.bits_per_sample//8))

    def playWav(self, wav_file):
        """
        label:
            en: ' %1 playWav, wav_file: %2'
        params:
            wav_file:
                name: wav_file
        """
        self.__setup(wav_file)
        self.counter = self.data_size//len(self.buffer)
        self.fd.seek(44)
        self.fd.readinto(self.buffer, len(self.buffer))
        self.tim.init(mode=Timer.ONE_SHOT, period=1000, callback=self.__timer_cb)
        Speaker.playRaw(self.buffer, self.sample_rate)

    def __continue_playback(self, x):
        """
        label:
            en: ' %1 continue_playback, _: %2'
        params:
            x:
                name: x
        """
        self.fd.readinto(self.buffer, len(self.buffer))
        Speaker.playRaw(self.buffer, self.sample_rate)

    def __timer_cb(self, _):
        """
        label:
            en: 'method %1 param1  param2 '
        """
        if self.counter &gt; 0:
            self.counter -= 1
            self.tim.init(mode=Timer.ONE_SHOT, period=1000, callback=self.__timer_cb)
            micropython.schedule(self.__continue_playback, 1)
</code></pre>
]]></description><link>https://community.m5stack.com/post/30692</link><guid isPermaLink="true">https://community.m5stack.com/post/30692</guid><dc:creator><![CDATA[tomfahey]]></dc:creator><pubDate>Wed, 25 Feb 2026 10:55:11 GMT</pubDate></item><item><title><![CDATA[Reply to cardputer use built in speaker to play wav file error on Mon, 28 Jul 2025 01:31:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/zhouyousong" aria-label="Profile: zhouyousong">@<bdi>zhouyousong</bdi></a> said in <a href="/post/29618">cardputer use built in speaker to play wav file error</a>:</p>
<blockquote>
<p dir="auto">Reply</p>
</blockquote>
<p dir="auto">hmmm i think that will be bit of hard, but we will keep trying it</p>
]]></description><link>https://community.m5stack.com/post/29627</link><guid isPermaLink="true">https://community.m5stack.com/post/29627</guid><dc:creator><![CDATA[kuriko]]></dc:creator><pubDate>Mon, 28 Jul 2025 01:31:33 GMT</pubDate></item><item><title><![CDATA[Reply to cardputer use built in speaker to play wav file error on Sat, 26 Jul 2025 12:24:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kuriko" aria-label="Profile: kuriko">@<bdi>kuriko</bdi></a> will the cardputer implement the background ability ? maybe in the future?</p>
]]></description><link>https://community.m5stack.com/post/29618</link><guid isPermaLink="true">https://community.m5stack.com/post/29618</guid><dc:creator><![CDATA[zhouyousong]]></dc:creator><pubDate>Sat, 26 Jul 2025 12:24:15 GMT</pubDate></item><item><title><![CDATA[Reply to cardputer use built in speaker to play wav file error on Mon, 21 Jul 2025 02:33:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/zhouyousong" aria-label="Profile: zhouyousong">@<bdi>zhouyousong</bdi></a><br />
Currently, only CoreS3 and Box support using the ESP-ADF Audio Player library, and only these two devices support background audio playback.<br />
<a href="https://uiflow-micropython.readthedocs.io/en/latest/system/audio.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://uiflow-micropython.readthedocs.io/en/latest/system/audio.html</a><br />
<img src="/assets/uploads/files/1753065219487-c9d68f67-6ada-415e-873e-1c4fc241169a-image.png" alt="c9d68f67-6ada-415e-873e-1c4fc241169a-image.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://community.m5stack.com/post/29556</link><guid isPermaLink="true">https://community.m5stack.com/post/29556</guid><dc:creator><![CDATA[kuriko]]></dc:creator><pubDate>Mon, 21 Jul 2025 02:33:58 GMT</pubDate></item><item><title><![CDATA[Reply to cardputer use built in speaker to play wav file error on Sun, 20 Jul 2025 12:21:33 GMT]]></title><description><![CDATA[<p dir="auto">currently I can play wav file, but I found it cannot run in background, if I started one wav, loop will be blocked till this wav finished. <img src="/assets/uploads/files/1753014057102-3e965176-d6a9-41d1-9178-1348c0263903-image.png" alt="3e965176-d6a9-41d1-9178-1348c0263903-image.png" class=" img-fluid img-markdown" /><br />
I have tried to change the priority. any one know how to fix this ?</p>
]]></description><link>https://community.m5stack.com/post/29550</link><guid isPermaLink="true">https://community.m5stack.com/post/29550</guid><dc:creator><![CDATA[zhouyousong]]></dc:creator><pubDate>Sun, 20 Jul 2025 12:21:33 GMT</pubDate></item></channel></rss>