Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

PlaySound() Cannot Play Multiple Times

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

    PlaySound() Cannot Play Multiple Times

    I write a simple code like this, but the sound cannot play more than once.
    Could you suggest any idea to let the sound play multiple times?

    Thanks.

    (FileName is a property referring to the name of wav file)
    Code:
    protected override void Initialize() {
            Overlay				= true;
    	CalculateOnBarClose = false;
    }
    
    protected override void OnBarUpdate() {
            alertCalls = 2;
    	while (alertCalls > 0) {
    				PlaySound(FileName);
    				PlaySound(FileName);
    				PlaySound(FileName);
    				alertCalls--;
    	}  
    }

    #2
    Hi Tracoder,

    Thank you for your post.

    You can code for multiple sounds to be played in a loop, however NinjaTraders sound player will not be able to handle the sounds that quickly from the code. The last call will overwrite the rest and get played once as I have confirmed this on my end.

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thank you for your knowledge.

      So, what is your recommended solution if I want a "ding" sound to be played continuously for 10 times, that is "ding ding ding ding ding......"? Will I have to employ a Timer?

      THanks!

      Comment


        #4
        Tracoder,

        That would be the best solution to incorporate with getting the multiple sounds to play.

        http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Record a new file with the sound.
          You have a microphone And speakers hopefully.

          Originally posted by tracoder View Post
          I write a simple code like this, but the sound cannot play more than once.
          Could you suggest any idea to let the sound play multiple times?

          Thanks.

          (FileName is a property referring to the name of wav file)
          Code:
          protected override void Initialize() {
                  Overlay				= true;
          	CalculateOnBarClose = false;
          }
          
          protected override void OnBarUpdate() {
                  alertCalls = 2;
          	while (alertCalls > 0) {
          				PlaySound(FileName);
          				PlaySound(FileName);
          				PlaySound(FileName);
          				alertCalls--;
          	}  
          }

          Comment


            #6
            Originally posted by tracoder View Post
            Thank you for your knowledge.

            So, what is your recommended solution if I want a "ding" sound to be played continuously for 10 times, that is "ding ding ding ding ding......"? Will I have to employ a Timer?

            THanks!
            Include a delay in your loop.

            Comment


              #7
              Originally posted by NinjaTrader_Cal View Post
              Tracoder,

              That would be the best solution to incorporate with getting the multiple sounds to play.
              Thank you for letting me know the best solution.

              Originally posted by sledge
              Record a new file with the sound.
              You have a microphone And speakers hopefully.
              Thanks for joining my topic However, my intention is to let a sound (selected by users) play non-stop until it is turned off. So, we cannot record a new file here.

              Originally posted by koganam
              Include a delay in your loop.
              Thank koganam! So, you suggest playing around with Thread.Sleep(), right?

              Comment


                #8
                Originally posted by tracoder View Post
                Thank you for letting me know the best solution.



                Thanks for joining my topic However, my intention is to let a sound (selected by users) play non-stop until it is turned off. So, we cannot record a new file here.



                Thank koganam! So, you suggest playing around with Thread.Sleep(), right?
                As a general rule, you do not want to pause a thread, because it interferes with NT processing.

                From the way that you pose the question, I have to presume that you are using COBC = false. In that case all you need to do is store the current time, use DateTime.Now.AddSeconds(double delayLength), and check that on the next bar update, the time exceeds the delayLength and then play the sound again.

                Actually that will also work with COBC = true, with the understanding that in that case, the sound will be gated by the COBC value to play only once per bar. If you are using COBC = true, and still want to play sounds intrabar, you will either have to use a finer granularity secondary barSeries to key off of, or use another event that is not gated by COBC; such as OnMarketData().
                Last edited by koganam; 12-10-2013, 07:29 PM.

                Comment


                  #9
                  Originally posted by koganam View Post
                  As a general rule, you do not want to pause a thread, because it interferes with NT processing.

                  From the way that you pose the question, I have to presume that you are using COBC = false. In that case all you need to do is store the current time, use DateTime.Now.AddSeconds(double delayLength), and check that on the next bar update, the time exceeds the delayLength and then play the sound again.

                  Actually that will also work with COBC = true, with the understanding that in that case, the sound will be gated by the COBC value to play only once per bar. If you are using COBC = true, and still want to play sounds intrabar, you will either have to use a finer granularity secondary barSeries to key off of, or use another event that is not gated by COBC; such as OnMarketData().
                  Thank you, koganam. Reading your helps always inspires me.

                  The approach for COBC = false is clear to me.

                  However, for COBC = true, I have questions for both of your recommendations:
                  - Use 2nd bar series: because we don't know what time frame the user uses (at the Initialize state), how can we add a relevant bar series?
                  - Use OnMarketData(): the notes say that this method is just reserved for advanced programmers. Sound a little dangerous . From your experiences, is the method "dangerous" as it seems to be, and does it cause a lot of burdens for CPUs?

                  Thanks.

                  P.S. This problem seems to be a simple one, but I have found it not as simple as it seems .
                  Last edited by tracoder; 12-11-2013, 03:16 AM.

                  Comment


                    #10
                    Originally posted by tracoder View Post
                    Thank you, koganam. Reading your helps always inspires me.

                    The approach for COBC = false is clear to me.

                    However, for COBC = true, I have questions for both of your recommendations:
                    - Use 2nd bar series: because we don't know what time frame the user uses (at the Initialize state), how can we add a relevant bar series?
                    - Use OnMarketData(): the notes say that this method is just reserved for advanced programmers. Sound a little dangerous . From your experiences, is the method "dangerous" as it seems to be, and does it cause a lot of burdens for CPUs?

                    Thanks.

                    P.S. This problem seems to be a simple one, but I have found it not as simple as it seems .
                    What's your idea, koganam?

                    Comment


                      #11
                      Originally posted by tracoder View Post
                      Thank you, koganam. Reading your helps always inspires me.

                      The approach for COBC = false is clear to me.

                      However, for COBC = true, I have questions for both of your recommendations:
                      - Use 2nd bar series: because we don't know what time frame the user uses (at the Initialize state), how can we add a relevant bar series?
                      - Use OnMarketData(): the notes say that this method is just reserved for advanced programmers. Sound a little dangerous . From your experiences, is the method "dangerous" as it seems to be, and does it cause a lot of burdens for CPUs?

                      Thanks.

                      P.S. This problem seems to be a simple one, but I have found it not as simple as it seems .
                      Sorry for the late response.
                      - Use 2nd bar series: because we don't know what time frame the user uses (at the Initialize state), how can we add a relevant bar series?
                      What about a 1-tick barsSeries? Then you have a secondary bar series that is activated on every tick; in effect, one that is behaving exactly like COBC = false, as it is responding to every tick.
                      - Use OnMarketData(): the notes say that this method is just reserved for advanced programmers. Sound a little dangerous . From your experiences, is the method "dangerous" as it seems to be, and does it cause a lot of burdens for CPUs?
                      What or who is an advanced programmer? If you understand the method and how it fits into the framework, you use it. It is just another event, which responds to every tick.
                      Last edited by koganam; 12-12-2013, 10:27 PM.

                      Comment


                        #12
                        Originally posted by tracoder View Post
                        What's your idea, koganam?
                        It depends on how many charts you have going, Internet speed, cpu...

                        On market data isn't all that bad. .. Unless you're doing crazy computations..

                        Comment


                          #13
                          Originally posted by sledge View Post
                          It depends on how many charts you have going, Internet speed, cpu...

                          On market data isn't all that bad. .. Unless you're doing crazy computations..
                          Thank you sledge. You give me more confidence

                          Comment


                            #14
                            Originally posted by koganam View Post
                            Sorry for the late response.

                            What about a 1-tick barsSeries? Then you have a secondary bar series that is activated on every tick; in effect, one that is behaving exactly like COBC = false, as it is responding to every tick.

                            What or who is an advanced programmer? If you understand the method and how it fits into the framework, you use it. It is just another event, which responds to every tick.
                            Thank you, koganam. You have clarifed everything for me . Sincere thanks for your kind help!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by PaulMohn, Today, 12:36 PM
                            2 responses
                            16 views
                            0 likes
                            Last Post PaulMohn  
                            Started by Conceptzx, 10-11-2022, 06:38 AM
                            2 responses
                            53 views
                            0 likes
                            Last Post PhillT
                            by PhillT
                             
                            Started by Kaledus, Today, 01:29 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post Kaledus
                            by Kaledus
                             
                            Started by yertle, Yesterday, 08:38 AM
                            8 responses
                            37 views
                            0 likes
                            Last Post ryjoga
                            by ryjoga
                             
                            Started by rdtdale, Today, 01:02 PM
                            1 response
                            6 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Working...
                            X