Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

alerter sound file
 Post subject: alerter sound file Post rating: 0   New post Posted: Tue 31 May, 2011, 13:39 

User rating: 0
Joined: Tue 31 May, 2011, 11:13
Posts: 4
Location: United Kingdom,
Hi,

I am trying to change the sound for 'Alerter.java' but if I do not use the 'sample.wav' file downloaded from here, I get the following error:

12:31:34 Strategy "Alerter" is stopped at 2011-05-31 12:31:34.009 GMT on the local computer with parameters "Alarm file"=[C:\Users\Username\Documents\JForex\Strategies\filename.wav]
12:31:34 javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file @ com.dukascopy.jforex.Alerter.playSound(Alerter.java:180)

The filetype is a wav file, file permissions are the same as the sound file that works, but it will not work with any other sound file (only sample.wav).

Any help much appreciated. Many thanks.


 
 Post subject: Re: alerter sound file Post rating: 0   New post Posted: Wed 08 Jun, 2011, 12:39 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
It does work with other .wav files, consider a bit smaller IStrategy example which plays alert every 10 seconds:
package jforex.codebase.test;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.sound.sampled.*;

import com.dukascopy.api.*;

@RequiresFullAccess
public class AlerterMini implements IStrategy {
   private IContext context;
   
   private Instrument instrument = Instrument.EURUSD;
   private Period period = Period.TEN_SECS;

   @Configurable("Alarm file 1")
   public File alarmFile1 = new File("C:\\temp\\duck.wav");
   
   @Configurable("Alarm file 2")
   public File alarmFile2 = new File("C:\\temp\\bird.wav");

   private SimpleDateFormat sdf;

   public void onAccount(IAccount account) throws JFException {
   }

   public void onMessage(IMessage message) throws JFException {
   }

   public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
      if (!this.period.equals(period) || !this.instrument.equals(instrument))
         return;
      print("play: " + sdf.format(bidBar.getTime()));
      playSound(alarmFile1);
   }

   public void onStart(IContext context) throws JFException {
      this.context = context;
      
      String DATE_FORMAT_NOW = "HH:mm:ss SSS"; //yyyy-MM-dd
      sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
      
      print("start");
      
      playSound(alarmFile2);
   }

   public void onStop() throws JFException {}

   public void onTick(Instrument instrument, ITick tick) throws JFException {   }


   private void playSound(File wavFile) throws JFException {
      try {
         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(wavFile);
         AudioFormat af = audioInputStream.getFormat();
         int nSize = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
         byte[] audio = new byte[nSize];
         DataLine.Info info = new DataLine.Info(Clip.class, af, nSize);
         audioInputStream.read(audio, 0, nSize);
         Clip clip = (Clip) AudioSystem.getLine(info);
         clip.open(af, audio, 0, nSize);
         clip.start();
      } catch (Exception e) {
         e.printStackTrace();
         print("errror on play: " + e.getMessage());
         //throw new JFException(e);
      }
   }
   
   private void print(Object o){
      context.getConsole().getOut().println(o);
   }
}
Find the example .wav files in attachments


Attachments:
duck.wav [802.92 KiB]
Downloaded 379 times
bird.wav [307.03 KiB]
Downloaded 385 times
DISCLAIMER: Dukascopy Bank SA's waiver of responsability - Documents, data or information available on this webpage may be posted by third parties without Dukascopy Bank SA being obliged to make any control on their content. Anyone accessing this webpage and downloading or otherwise making use of any document, data or information found on this webpage shall do it on his/her own risks without any recourse against Dukascopy Bank SA in relation thereto or for any consequences arising to him/her or any third party from the use and/or reliance on any document, data or information found on this webpage.
 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com