Switching Between Music On Hold Streams. [13.8.2]

Home » Asterisk Users » Switching Between Music On Hold Streams. [13.8.2]
Asterisk Users 16 Comments

I’d like multiple people to be able to dial in and listen to various live radio streams.

I was told that the correct resource-friendly way would be to setup a MoH class, and then select that from the dialplan.

This works well, but how do I switch between streams?

Someone correct me if I’m wrong, but from previous similar questions a few years ago it seems like once you’ve entered a MoH class, there is no exit.

But might there be some trick involved merged or bridged calls, or chan_spy or something, so that callers could quickly switch between 3
streams with a keypress?

Thank you!

16 thoughts on - Switching Between Music On Hold Streams. [13.8.2]

  • Michael,

    What you do is you dial another context and then use the G option in the dial string. So something like this.

    Exten => s,1,answer Exten => s,2,Background(play-menu)
    Exten => s,3,waitexten Exten => S,4,Goto(s,2)

    Exten => 1,1,Dial(Local/CNN@play-radio)
    Exten => 1,2,Goto(s,2)

    Exten => 2,1,Dial(Local/NPR@play-radio)
    Exten => 2,2,Goro(s,2)

    Exten => _[A-Z].,1,Answer Exten => _[A-Z].,2,Musiconhold(${EXTEN})



    —–Original Message—

  • Hi there;

    I didn’t see any “G” option in the example above, and the usage for the option parameters is entirely undocumented at https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Application_Dial

    The G options are as below G – If the call is answered, transfer the calling party to the specified priority and the called party to the specified priority plus one. context exten priority

    I think I have something almost there now, with the following:

    exten => s,1,Answer exten => s,2,BackGround(menu)
    exten => s,3,WaitExten exten => s,4,Goto(s,2)
    exten => _[2,3,4,5],1,Dial(Local/${EXTEN}@play-radio,,G(play-radio^${EXTEN}^2))
    exten => _[2,3,4,5],2,Goto(s,2)

    Exten => _[2,3,4,5],1,Answer exten => _[2,3,4,5],2,Music

  • Jonathan H wrote:

    You can configure music classes with a DTMF digit that when pressed when listening to music on hold will cause the channel to switch to the new music class.

    From the musiconhold.conf sample file:

    ;digit=# ; If this option is set for a class, then when callers ;
    ; are
    ; ; listening to music on hold, they can press this
    ; ; digit, and
    ; ; they will switch to listening to this music class.


  • Sorry about that it’s the g option:
    g: Proceed with dialplan execution at the next priority in the current
    extension if the destination channel hangs up.

    What you are doing is “dialing” another location that picks up. When you press # the called leg hangs up and the call continues in the dialplan.

  • Josh,

    As he is playing different streams I think he wants to give the users the option to select another stream.

  • Dovid Bender wrote:

    Each stream should be a separate music class, so switching between using the DTMF feature would still work – unless I’m misunderstanding what is trying to be achieved here.

  • Josh,

    He mentioned that he is building something for radio stations for people to listen into. They want to be able to select what they hear. I recently built a system where people could call and listen to rail road traffic. Using the g option in the dial string + # was the only way to let them select.

  • Dovid Bender wrote:

    Right, and since the subject of this thread is “Music on Hold streams” I
    assumed he was doing that through a music class for each stream.

  • You have an error in your dialplan! The pattern _[2,3,4,5] will match any of
    2, a comma, 3, a comma (again), 4, a comma or 5.

    I think you might mean _[2345] which will match any of 2, 3, 4 or 5 (but not a comma), and contains no tautologies.

  • Thanks Joshua and everyone,

    Joshua’s solution seems a lot simpler and works well.

  • Jonathan H wrote:

    That’s correct I’m afraid. If you need even more, then without modifying the code that option won’t work for you.


  • Thanks Joshua,

    I realise mine is a bit of a niche case, so I certainly don’t expect to be catered for “out of the box” as it were, but is there some way Dovid’s solution could be wrangled into doing what I require then?
    Dovid mentions this (lower case!) g option, but call me thick, but I’m not sure how it’s supposed to work. I’m not being lazy here, but I
    really just can’t figure it out!

    I’ve tried a few other things, but it really doesn’t seem like the second channel can “hear” or respond to the DTMF tones.

    It doesn’t have to be an elegant solution, any fudge will do!

    It did just occur to me that I could use this option I just noticed:

    “H – Allow the calling party to hang up by sending the DTMF sequence defined for disconnect in features.conf.”

    Will that hang up the whole call, or just the extension which was dialled by the Dial(PJSIP/…) application? Does that make sense?
    (It’s getting late here!).


  • Jonathan H wrote:

    You could try using H and g together as you’ve mentioned… I’m not sure if it would actually hang up the calling party or not…

    Otherwise I don’t have any ideas, stuff wasn’t written or designed for this use case even when fudged together.

  • If you ever figure out AAC in Asterisk for MOH let me know. The ones that I
    have working is MP3 and MMS.

  • Hello!

    I thought having finally “cracked it”, I might as well post what I’ve done.


    Can someone please take a quick look and see if there’s anything I could have done better or more efficiently, or if anything stands out as particularly horrific?

    Basically, it uses an app called crudini to add sections to musiconhold.conf, then does an moh reload.

    When the user has finished listening and presses * then the remote extension is dropped and the caller returns to the current menu.

    The nice thing about this is that even if two callers call and listen to the same moh stream, when one hangs up, even though it deletes the config and reloads moh, Asterisk is nice to the other caller and they keep listening.

    The end result is what I wanted, which is to not have any extra CPU load or network usage when no-one is listening. And if more than one person is listening, it’s still only “using” one remote stream, as I’ve uncommented the cachertclasses value.