Asterisk中如何实现按#键中断Monitor()命令的音频录制?
Monitor() with DTMF # Key Great question! Unlike Record(), Asterisk's Monitor() command doesn’t have a built-in parameter to stop recording when a DTMF key is pressed. But we can add this functionality with straightforward dialplan logic—here’s how to implement it:
Core Approach
We’ll start the Monitor() process, then listen for DTMF input using WaitExten() (or Background() if you need to play audio prompts). When the # key is detected, we’ll trigger StopMonitor() to halt the recording immediately.
Dialplan Example
Here’s a complete, tested dialplan snippet you can adapt:
[monitor-with-dtmf-stop] exten => s,1,NoOp(Initiating call recording with # stop trigger) ; Start monitoring - adjust format (wav/gsm), filename, and options as needed ; The "mb" option mixes both caller and callee audio into a single file (optional) exten => s,n,Monitor(wav,call_recording_${UNIQUEID},mb) ; Listen for DTMF input for 3600 seconds (1 hour) - use 0 for infinite wait exten => s,n,WaitExten(3600) ; Handle the # DTMF key press exten => #,1,NoOp(# key detected - stopping recording) exten => #,n,StopMonitor() ; Optional: Add logic here to continue the call or hang up exten => #,n,Hangup() ; Handle timeout (no DTMF pressed within the wait period) exten => t,1,NoOp(Timeout reached - stopping recording) exten => t,n,StopMonitor() exten => t,n,Hangup()
Key Details & Customizations
- Dynamic Filenames: Using
${UNIQUEID}ensures each call gets a unique recording filename, avoiding overwrites. - DTMF Detection: Ensure your channel configuration (e.g.,
sip.conffor SIP trunks) has the correctdtmfmodeset (common values arerfc2833orinfo). - Playing Prompts: If you need to play an audio prompt while monitoring (e.g., "Press # to stop recording"), replace
WaitExten(3600)withBackground(your_prompt_file)—the # extension will still trigger when pressed. - Infinite Wait: Use
WaitExten(0)instead of3600if you want to listen for DTMF until the call ends or the # key is pressed.
Troubleshooting Tip
If DTMF isn’t triggering the # extension, check Asterisk’s logs (/var/log/asterisk/full) to confirm DTMF events are being detected. You can also add Verbose(3) in the dialplan to debug DTMF input in real-time.
内容的提问来源于stack exchange,提问作者eran

