final ev3 stuff
[des2015.git] / mart / ev3 / ex1 / nl / ru / des / WavPlayer.java
index 1a3b55e..2e0760f 100644 (file)
@@ -1,6 +1,10 @@
 package nl.ru.des;
 
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 
 import lejos.hardware.Audio;
 import lejos.utility.Delay;
@@ -15,20 +19,49 @@ public class WavPlayer extends Thread{
        
        public WavPlayer(Audio audio){
                this.audio = audio;
-               audio.setVolume(100);
+               audio.setVolume(Audio.VOL_MAX);
+       }
+       
+       public void preLoad(String[] files){
+               LCDPrinter.print("Preloading audiofiles...");
+               for(String sound : files){
+                       try {
+                               LCDPrinter.print("Preloading: " + sound);
+                               InputStream inp = new BufferedInputStream(Class.class.getResourceAsStream("/nl/ru/des/sounds/" + sound));
+                               File f = new File(sound);
+                               if(f.exists()){
+                                       inp.close();
+                                       LCDPrinter.print(sound + " already exists, skipping...");
+                                       continue;
+                               }
+                               FileOutputStream outp = new FileOutputStream(sound);
+                               byte[] buffer = new byte[1024];
+                               int length;
+                               while ((length = inp.read(buffer)) > 0){
+                                       outp.write(buffer, 0, length);
+                               }
+                               outp.close();
+                               LCDPrinter.print("Done...");
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
        }
        
        @Override
        public void run(){
+               File c;
                while(true){
                        if(current != null){
-                               File c = new File(current);
+                               c = new File(current);
                                if(c.canRead()){
                                        audio.playSample(new File(current));
+                               } else {
+                                       LCDPrinter.print("can't load: " + current);
                                }
                                current = null;
                        }
-                       Delay.msDelay(200);
+                       Delay.msDelay(00);
                }
        }
 }
\ No newline at end of file