Announcement

Collapse
No announcement yet.

How to change Audio file triggered in an event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to change Audio file triggered in an event

    I am trying to create an event which will play an mp3 file, randomly selected from 5 different files. After some trials and tribulations, I created a short script that made a copy of the selected file. (i.e. "n.mp3" is copied to "outfile.mp3" where n=1 to 5) The event should then send this copied file to a specific speaker.

    I can see that the script correctly overwrites the newly selected file to outfile.mp3. But the "play audio file" command insists on sending the same file every time. I guess HS3 keeps the initially selected file in memory and, although the contents of "outfile.mp3" change, HS3 does not re-upload it.

    Hope my explanation is clear.

    Is my approach wrong, or can someone suggest a work-around?

    #2
    Maybe another approach:

    create separate files : 1.mp3, 2.mp3, etc., use a random number generator in your script and call your file based on the result.


    Code:
    Sub Main(ByVal Parms As Object)
    
    Randomize()
    ' Initialize the random-number generator. The Rnd function returns a random number. The number is always less than 1 but greater or equal to 0.
    
    
    Dim Z As Integer = Int((5 * Rnd()) + 1) ' Generate random value between 1 and 5.
    
    'Dim Z As Integer = Int((101 * Rnd()) ) ' Generate random value between 0 and 100.
    
    'Dim Z As Integer = Int((101 * Rnd()) + 25) ' Generate random value between 25 and 125.
    
    'hs.SetDeviceValueByRef(4466, Z, True)
    
    'hs.writelog ("Random","Value = " & Z )
    
    End Sub

    Comment


      #3
      I do have 5 diiferent mp3 files and select one randomly, but as far as I understand, the file that I call from the event has to be hard-coded. Which is why I copy the selected mp3 file to the hard-coded filename.

      Comment


        #4
        It may be easier to play the random MP3 file via the script.....
        Jon

        Comment


          #5
          That would make sense........but as a vb novice I'd appreciate some guidance !

          Comment


            #6
            You can use something like:

            Code:
            hs.SpeakEx(0, hs.GetAppPath & "\Media\n.mp3", True)
            Change the path to suit.
            Jon

            Comment


              #7
              Another approach I use for playing random MP3s or doing any event when the door opens during Christmas is I store the names of all events in a comma-separated GlobalVar string. When I need to trigger, it parses the string into an array and uses the random number to select which event to run from the array. In your case, for MP3 playing only, you'd have to have each MP3 triggered by a manual event called by this script. You don't change the MP3 file names, just have an event for each.

              As Jon00 and others have stated, this kind of approach requires a script. Here's mine but it has a few twists:

              First, I save the last random chosen and generate new random numbers so it does not repeat. It also has the ability to trigger more than one by setting the Qty parameter in the event.

              The event is called when the front door opens during the Christmas season.


              Code:
              Sub RunRandomEvents(qty)
                 'This subroutine requires the creation of a Global Variable called "lastrandom" of type integer and
                 'Global Variable xmaseventslist of type string containing comma-delimited list of event names.
                 'This is generic so that it could be used to run any random list of HS events, not just play MP3s.
                 'It could play a song on one trigger, but make a train run around the Christmas tree on the next trigger, and
                 'make some light show run on the next.
              
              'For music, each MP3 song has its own event since each is a peer to other events that may fire
              
                 Randomize()
                 Dim Events() As String
                 Events = Split(hs.GetVar("xmaseventslist"),",")
                 Dim i As Integer
                 Dim r As Integer
                 Dim lastr As Integer
                 lastr = hs.GetVar("lastrandom")
              
                 For i = 1 to qty
                     Do
                       r = Int(Rnd()*(UBound(Events)))
                     Loop While (r = lastr)
                    'Now we have a new random number and we'll call the event listed (dynamic event call of existing event names)
                     hs.TriggerEvent(Events(r))
                     lastr = r
                 Next
              
                 hs.SaveVar("lastrandom",lastr)
              End Sub​

              Comment


                #8
                Originally posted by Lio View Post
                I am trying to create an event which will play an mp3 file, randomly selected from 5 different files. After some trials and tribulations, I created a short script that made a copy of the selected file. (i.e. "n.mp3" is copied to "outfile.mp3" where n=1 to 5) The event should then send this copied file to a specific speaker.

                I can see that the script correctly overwrites the newly selected file to outfile.mp3. But the "play audio file" command insists on sending the same file every time. I guess HS3 keeps the initially selected file in memory and, although the contents of "outfile.mp3" change, HS3 does not re-upload it.

                Hope my explanation is clear.

                Is my approach wrong, or can someone suggest a work-around?
                I have no idea if my below comment is related to your problem, but put it out there incase it helps:

                I ran into a similar problem when using the SpeakFile to HS "Speakers". I would run the SpeakFile using memory location X, then immediately load a new file into memory location X and repeat the SpeakFile action and it would play the previous audio file, UNLESS I waited a minute or so, then it would play the newer one. I attributed the observation to "caching" somewhere along the line getting to my tablets speaker. Never solved the problem and abandon my simple process.

                Comment


                  #9
                  Guys - many thanks for all your very prompt suggestions. As ever, this forum been of great help. My barking dog now has a great repertoire of random warning noises.

                  Comment


                    #10
                    Good news, Would you mind sharing your final solution? Could be useful for future reference. Thx

                    Comment


                      #11
                      I first tried Jon00's suggestion to use hs.Speak0, but this doesn't seem to allow sending the mp3 to a specific speaker. Instead, using a simplified version of Dzee's suggestion, I created 5 different manually triggered events (barkn with n=1 to 5), which fire off a specific mp3 file to the desired speaker via the Chromecast PI. My motion-triggered event has a random number generator that selects one of the bark files and triggers the appropriate event. I still need to modify my random number generator as per Dzee's method to avoid selecting the same bark in successive calls.

                      Comment


                        #12
                        Originally posted by Lio View Post
                        I first tried Jon00's suggestion to use hs.Speak0, but this doesn't seem to allow sending the mp3 to a specific speaker. Instead, using a simplified version of Dzee's suggestion, I created 5 different manually triggered events (barkn with n=1 to 5), which fire off a specific mp3 file to the desired speaker via the Chromecast PI. My motion-triggered event has a random number generator that selects one of the bark files and triggers the appropriate event. I still need to modify my random number generator as per Dzee's method to avoid selecting the same bark in successive calls.
                        Sorry, the speaker client used is optional so you could have used:
                        Code:
                        hs.SpeakEx(0, hs.GetAppPath & "\Media\n.mp3", True,"<host>:<instance>")
                        Jon

                        Comment


                          #13
                          Another alternative, which I've been using for a few years without problems, even with HS4, is the plugin by our dear friend CFGuy: Random Phrase

                          Comment


                            #14
                            Jon00 - thanks for pointing that out - makes my script much simpler now and no need for multiple events.

                            As for the Random Phrase plugin - I can't see it in Utilities - but thanks anyway.

                            Comment


                              #15
                              I do see it here.

                              Click image for larger version

Name:	Screenshot_20230121_100451_Chrome.jpg
Views:	84
Size:	30.0 KB
ID:	1588000

                              Comment

                              Working...
                              X