Sound notifications not working in indicator
Sound notifications not working in indicator
19 Apr 2021, 03:55
Hi,
I am having trouble playing sound notifications in an indicator in the latest version of Ctrader 4.0.
Yes, I have sound enabled and notifications on Ctrader and Filesystem access is enabled. I have
also converted this code to a cBot and the sound works fine. What could be causing this issue?
See demonstration code below.
using System;
using System.IO;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
public class TestSound : Indicator
{
[Parameter("Enable Sound Alerts", Group = "Sound", DefaultValue = false)]
public bool soundAlerts { get; set; }
[Parameter("Sound Filename", Group = "Sound", DefaultValue = "foghorn.mp3")]
public string filename { get; set; }
private string path;
private bool notified = false;
protected override void Initialize()
{
try
{
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Path.Combine(desktopFolder, filename);
Print(path);
} catch (Exception error)
{
Print("Error: " + error);
}
PlaySound();
}
public override void Calculate(int index)
{
if (!notified)
{
notified = true;
PlaySound();
}
}
private void PlaySound()
{
if (soundAlerts)
{
try
{
Notifications.PlaySound(path);
} catch (Exception error)
{
Print("Error: " + error);
}
}
}
}
}
Replies
sascha.dawe
22 Apr 2021, 13:06
RE: RE:
Thanks Firemyst,
This appears to be the issue. I added a time delay of 10 seconds and the sound played fine. So it appears, that while loading the bars on start up, caused interference with the sound notification on the indicator.
Cheers,
Sascha
firemyst said:
sascha.dawe said:
Hi,
I am having trouble playing sound notifications in an indicator in the latest version of Ctrader 4.0.
Yes, I have sound enabled and notifications on Ctrader and Filesystem access is enabled. I have
also converted this code to a cBot and the sound works fine. What could be causing this issue?
See demonstration code below.
using System; using System.IO; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)] public class TestSound : Indicator { [Parameter("Enable Sound Alerts", Group = "Sound", DefaultValue = false)] public bool soundAlerts { get; set; } [Parameter("Sound Filename", Group = "Sound", DefaultValue = "foghorn.mp3")] public string filename { get; set; } private string path; private bool notified = false; protected override void Initialize() { try { var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); path = Path.Combine(desktopFolder, filename); Print(path); } catch (Exception error) { Print("Error: " + error); } PlaySound(); } public override void Calculate(int index) { if (!notified) { notified = true; PlaySound(); } } private void PlaySound() { if (soundAlerts) { try { Notifications.PlaySound(path); } catch (Exception error) { Print("Error: " + error); } } } } }
Have you tried stepping through your code in Visual Studio debug mode to see what happens?
Also, because ticks could be coming in rather quickly, the system might be trying to play, but then gets stopped immediately when the next tick comes in because the "notified" variable is never set to true. I've had that happen before when trying to play sounds too quickly in succession.
Finally, what's the Print/logging output?
@sascha.dawe
firemyst
23 Apr 2021, 03:43
Glad that helped!
Something else to try is using IsLastBar such as follows:
if (!notified && IsLastBar())
{
notified = true;
PlaySound();
}
so it only plays on the last bar. If you're watching the charts live, this will be the most current bar. Thus you don't have to worry about the sound being played on any previous bars.
@firemyst
firemyst
22 Apr 2021, 09:21
RE:
sascha.dawe said:
Have you tried stepping through your code in Visual Studio debug mode to see what happens?
Also, because ticks could be coming in rather quickly, the system might be trying to play, but then gets stopped immediately when the next tick comes in because the "notified" variable is never set to true. I've had that happen before when trying to play sounds too quickly in succession.
Finally, what's the Print/logging output?
@firemyst