AudioPlayer Unit
This is the driver library of AudioPlayer Unit, which is used to play audio files.
Support the following products:
UiFlow2 Example
play audio
Open the audioplayer_core2_example.m5f2 project in UiFlow2.
This example plays the audio file on the AudioPlayer Unit.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
play audio
This example plays the audio file on the AudioPlayer Unit.
MicroPython Code Block:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from unit import AudioPlayerUnit 9import time 10 11 12title0 = None 13label0 = None 14label1 = None 15label2 = None 16audioplayer_0 = None 17 18 19play_state = None 20 21 22def btn_b_was_pressed_event(state): 23 global title0, label0, label1, label2, audioplayer_0, play_state 24 if play_state: 25 audioplayer_0.pause_audio() 26 else: 27 audioplayer_0.play_audio() 28 29 30def setup(): 31 global title0, label0, label1, label2, audioplayer_0, play_state 32 33 M5.begin() 34 Widgets.fillScreen(0x222222) 35 title0 = Widgets.Title( 36 "AudioPlayerUnit Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18 37 ) 38 label0 = Widgets.Label(">||", 145, 214, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 39 label1 = Widgets.Label("label1", 1, 71, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 40 label2 = Widgets.Label("label2", 1, 123, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 41 42 BtnB.setCallback(type=BtnB.CB_TYPE.WAS_PRESSED, cb=btn_b_was_pressed_event) 43 44 audioplayer_0 = AudioPlayerUnit(2, port=(33, 32)) 45 audioplayer_0.set_play_mode(0) 46 play_state = 0 47 48 49def loop(): 50 global title0, label0, label1, label2, audioplayer_0, play_state 51 M5.update() 52 play_state = audioplayer_0.check_play_status() 53 if play_state: 54 label1.setText(str("Play Status: Playing")) 55 else: 56 label1.setText(str("Play Status: Paused")) 57 label2.setText(str((str("Audio Num: ") + str((audioplayer_0.get_current_audio_number()))))) 58 time.sleep_ms(100) 59 60 61if __name__ == "__main__": 62 try: 63 setup() 64 while True: 65 loop() 66 except (Exception, KeyboardInterrupt) as e: 67 try: 68 from utility import print_error_msg 69 70 print_error_msg(e) 71 except ImportError: 72 print("please update to latest firmware")
Example output:
None
API
AudioPlayerUnit
- class unit.audioplayer.AudioPlayerUnit(id=2, port=None, verbose=False)
Bases:
objectCreate an AudioPlayerUnit object.
- Parameters:
UiFlow2 Code Block:

MicroPython Code Block:
from unit import AudioPlayerUnit audio_player_0 = AudioPlayerUnit(2, port=(33, 32))
- check_play_status()
Check the play status of the audio player.
- Returns:
The play status of the audio player.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.check_play_status()
- play_audio()
Play the audio.
- Returns:
The play status of the audio player.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.play_audio()
- pause_audio()
Pause the audio.
- Returns:
The play status of the audio player.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.pause_audio()
- stop_audio()
Stop the audio.
- Returns:
The play status of the audio player.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.stop_audio()
- next_audio()
Play the next audio.
- Returns:
Current play audio index.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.next_audio()
- previous_audio()
Play the previous audio.
- Returns:
Current play audio index.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.previous_audio()
- play_audio_by_index(index)
Play audio by index number.
- Parameters:
index (int) – The index of the audio to play.
- Returns:
Current play audio index.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.play_audio_by_index(1)
- play_audio_by_name(name)
Play audio by file name.
- Parameters:
name (str) – The name of the audio file to play.
- Returns:
Current play audio index.
- Return type:
MicroPython Code Block:
audio_player_0.play_audio_by_name("music.mp3")
- get_current_online_device_type()
Get the current online device type.
- Returns:
Device type code
- Return type:
int
- Device type:
1: USB
2: SD
3: UDISK or SD
4: Flash
5: Flash or UDISK
6: Flash or SD
MicroPython Code Block:
audio_player_0.get_current_online_device_type()
- get_current_play_device_type()
Get the current play device type.
- Returns:
Device type code (0: USB, 1: SD, 2: SPI FLASH).
- Return type:
MicroPython Code Block:
audio_player_0.get_current_play_device_type()
- get_total_audio_number()
Get the total number of audio files available.
- Returns:
The total number of audio files.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.get_total_audio_number()
- get_current_audio_number()
Get the current audio file number.
- Returns:
The current audio file number.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.get_current_audio_number()
- play_current_audio_at_time(time_min, time_sec)
Play the current audio from a specific time position.
- Parameters:
- Return type:
None
MicroPython Code Block:
audio_player_0.play_current_audio_at_time(1, 30)
- play_audio_at_time(audio_index, time_min, time_sec)
Play a specific audio file from a specific time position.
- Parameters:
- Return type:
None
MicroPython Code Block:
audio_player_0.play_audio_at_time(1, 0, 30)
- next_directory()
Navigate to the next directory.
MicroPython Code Block:
audio_player_0.next_directory()
- Return type:
None
- previous_directory()
Navigate to the previous directory.
MicroPython Code Block:
audio_player_0.previous_directory()
- Return type:
None
- end_audio()
End playing the current audio.
MicroPython Code Block:
audio_player_0.end_audio()
- Return type:
None
- get_file_name()
Get the name of the current audio file.
- Returns:
The name of the current audio file as a list of bytes.
- Return type:
MicroPython Code Block:
audio_player_0.get_file_name()
- select_audio_num(audio_num)
Select an audio file by number without playing it.
- Parameters:
audio_num (int) – The number of the audio file to select.
- Returns:
The current selected audio file number.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.select_audio_num(1)
- get_file_count()
Get the total number of files in the current directory.
- Returns:
The total number of files.
- Return type:
MicroPython Code Block:
audio_player_0.get_file_count()
- get_total_play_time()
Get the total play time of the current audio file.
- Returns:
A tuple containing (hour, minute, second) of the total play time.
- Return type:
MicroPython Code Block:
audio_player_0.get_total_play_time()
- decrease_volume()
Decrease the volume of the audio player.
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.decrease_volume()
- Return type:
None
- increase_volume()
Increase the volume of the audio player.
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.increase_volume()
- Return type:
None
- get_volume()
Get the current volume level of the audio player.
- Returns:
The current volume level.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.get_volume()
- set_volume(volume)
Set the volume level of the audio player.
- Parameters:
volume (int) – The volume level to set (0-30).
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.set_volume(15)
- repeat_at_time(start_min, start_sec, end_min, end_sec)
Set repeat playback between two time positions.
- Parameters:
- Return type:
None
MicroPython Code Block:
audio_player_0.repeat_at_time(0, 30, 1, 30)
- end_repeat()
End the repeat playback mode.
MicroPython Code Block:
audio_player_0.end_repeat()
- Return type:
None
- get_play_mode()
Get the current play mode.
- Returns:
The current play mode.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.get_play_mode()
- set_play_mode(mode)
Set the play mode.
- Parameters:
mode (int) – The play mode to set.
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
audio_player_0.set_play_mode(1)
- start_combine_play(mode, data)
Start combined play mode.
- Parameters:
- Return type:
None
MicroPython Code Block:
audio_player_0.start_combine_play(1, [1, 2, 3])
- end_combine_play()
End the combined play mode.
MicroPython Code Block:
audio_player_0.end_combine_play()
- Return type:
None

