4
$\begingroup$

I have this code. When I click on the clock, my alarm don't activate and not work. I have tried with DynamicModule and nothing. I don't know where is the error.

reloj = Dynamic[
   Refresh[ClockGauge[DateString["Time"], ImageSize -> 120, 
     LabelStyle -> {Darker@Black, 10.5, Bold}, 
     GaugeStyle -> {Red, Red, Black}, PlotTheme -> "Minimal", 
     TicksStyle -> Directive[Black, Bold], LabelStyle -> Bold, 
     GaugeLabels -> {"ROLEX", Placed[ "AMPM", {0.5, 0.35}]}, 
     GaugeMarkers -> "ThinArrowNeedle"], UpdateInterval -> 1]];

sound = EmitSound@
   Import[NotebookDirectory[] <> "alarmbeep.mp3", "Sound"];

alarma = Dynamic@
   Panel[Row[{Column[{Control[{{h, "", "Hours"}, Range[25] - 1}], 
        Control[{{m, "", "Minutes"}, Range[61] - 1}], 
        Control[{{oo, True, "Alarm"}, {True -> "ON", 
           False -> "OFF"}}]}], 
      If[Refresh[DateList[], UpdateInterval -> 1][[4 ;; 5]] == {h, 
          m} && oo, sound]}], Background -> LightBlue];

EventHandler[reloj, 
 "MouseClicked" :> 
  Quiet@CreateDialog[Column[{Panel[alarma], ChoiceButtons[]}], 
    WindowTitle -> "CONFIGURE LA ALARMA"]]
$\endgroup$
3
  • $\begingroup$ I guess sound should be defined using SetDelayed instead of Set as the EmitSound is part of the definition of sound. Or you should call EmitSound inside Dynamic. $\endgroup$
    – Johu
    Commented Sep 7, 2018 at 14:46
  • $\begingroup$ @Johu. When I called EmitSound inside Dynamic the sound don´t I can't stoped. This test I did it before and it did not work. $\endgroup$
    – Andres
    Commented Sep 7, 2018 at 16:01
  • $\begingroup$ Dynamic@Panel[ Row[{Column[{Control[{{h, "", "Hours"}, Range[25] - 1}], Control[{{m, "", "Minutes"}, Range[61] - 1}], Control[{{oo, True, "Alarm"}, {True -> "ON", False -> "OFF"}}]}], If[Refresh[DateList[], UpdateInterval -> 1][[4 ;; 5]] == {h, m} && oo, EmitSound@ Import[NotebookDirectory[] <> "alarmbeep.mp3", "Sound"]]}], Background -> LightBlue] $\endgroup$
    – Andres
    Commented Sep 7, 2018 at 16:05

1 Answer 1

6
$\begingroup$
  1. sound = EmitSound@... evaluates immediately and sound is just Null. You should've heard it after initial Shift+Enter. If you want it to be a 'reusable' sound you should use SetDelayed as suggested in comments:

    sound[] := EmitSound @ ...
    

    You don't need to use [] but I find it a good habbit to have functions/precedures called this way instead of just a symbol.

    Additional improvement, you can preimport the sound:

    With[ { s = Import[NotebookDirectory[] <> "alarmbeep.mp3", "Sound"] }
    , sound[]:=EmitSound @ s
    ]
    
  2. Your alarm trigger is in the dialog panel so it won't work when the dialog is hidden. You need to keep it visible with the clock. Something like:

    EventHandler[
      DynamicWrapper[reloj,  If[Refresh[...]]
    , "MouseClicked" :>... 
    ]
    
$\endgroup$
1
  • $\begingroup$ problem solved, thanks@Kuba $\endgroup$
    – Andres
    Commented Sep 14, 2018 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.