You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Streamlit的声学网站:点击录音按钮时同步触发Python音频播放函数的实现疑问

Syncing Audio Playback with st_audiorec's Start Recording in Streamlit

Great question! You absolutely don't need to bring Flask into the mix here—Streamlit's built-in session state and the st_audiorec component's behavior let you sync the start of recording with your sweep playback directly. Here's how to make it work:

Core Idea: Track Recording State with Streamlit Session State

Streamlit reruns your entire script every time a user interacts with a component, so we can use st.session_state to track when the recording starts (and avoid triggering playback multiple times accidentally).

Step-by-Step Implementation

First, let's adjust your existing functions and add session state logic:

  1. Update Your Playback Function for Browser Compatibility
    Instead of using server-side audio players (which won't work for remote users), use Streamlit's native st.audio with autoplay=True to play the sweep directly in the user's browser:

    import streamlit as st
    import soundfile as sf
    from st_audiorec import st_audiorec
    
    def play_sweep(wavefile_name):
        # Read the WAV file and play it in the browser
        audio_data, sample_rate = sf.read(wavefile_name)
        st.audio(audio_data, sample_rate=sample_rate, autoplay=True)
    
  2. Add Session State to Track Recording Triggers
    Initialize a session state variable to keep track of whether we've already triggered the sweep playback for the current recording session:

    # Initialize session state if it doesn't exist
    if "recording_active" not in st.session_state:
        st.session_state.recording_active = False
    
  3. Sync Recording Start with Playback
    When the user clicks "Start Recording", the st_audiorec component will start returning audio data (or a status dict, depending on your version). We can detect this change and trigger playback only once per recording session:

    # Render the audio recorder component
    recorded_audio = st_audiorec()
    
    # Check if recording has just started (audio data exists but we haven't played the sweep yet)
    if recorded_audio is not None and not st.session_state.recording_active:
        # Mark recording as active to avoid replaying the sweep
        st.session_state.recording_active = True
        # Trigger your sweep playback
        play_sweep("path/to/your/sweep.wav")
    
    # Reset state when recording stops (audio data is None again)
    if recorded_audio is None and st.session_state.recording_active:
        st.session_state.recording_active = False
    

For Newer st_audiorec Versions

If you're using a version of st_audiorec that returns a dictionary with a recording boolean (instead of raw audio data), adjust the check to look for that state:

audio_status = st_audiorec()

if audio_status.get("recording") and not st.session_state.recording_active:
    st.session_state.recording_active = True
    play_sweep("path/to/your/sweep.wav")

Key Notes

  • Browser-Side Playback: Using st.audio ensures the sweep plays directly for the user, even if your app is deployed to a remote server (server-side players like playsound won't work for hosted apps).
  • Avoid Duplicate Playback: The session state variable prevents Streamlit's rerun behavior from triggering the sweep multiple times during a single recording session.
  • Component Behavior: st_audiorec returns None when no recording is active, and starts returning data (or a "recording=True" status) as soon as the user clicks "Start Recording"—this is the trigger we use to sync playback.

This approach keeps your code streamlined, stays within the Streamlit ecosystem, and eliminates the need for Flask entirely.

内容的提问来源于stack exchange,提问作者Ettore Carlessi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 19:17:27