How to play music WHILE receiving DTMF characters

I need uninterrupted background music to be playing while I receive DTMF characters from calling party. I am using phpagi and the closest way I found to do this is as such:

$agi->set_music();
$count = 0;
$dtmf = "";
while (true) {
    $result = $AGI->wait_for_digit(4000);
    
    if ($result['result'] > 0){
        $dtmf = $dtmf . chr($result['result']);
    }
    else{
        break;
    }
}

The problem with this is that as the DTMF characters are being dialed (by an android application), there is a silent intermittent break in the music until all the DTMF characters are dialed and then the music is consistent again (i.e. music, 0.5sec stop, music, 0.5sec stop… etc.)

Things I’ve tried:
Will play music but will stop completely when the first DTMF is received

Will play music but the command blocks until its done (and thus doesn’t receive the DTMF)

Please understand that your requirement may result in poor decoding of the DTMF, as there will always be some echo of the outgoing music, which can confuse the decoder. In fact, UK dialtone had to change when DTMF was introduced, because it was harmonic rich and would have jammed dialing.

I will deal with the consequences (if any) after I’ve implemented a solution if one exists. Has anyone been able to do this successfully?

No one has any ideas?

Try to inject the music with the ChanSpy app.

stackoverflow.com/questions/8099 … lternative