final ev3 stuff
[des2015.git] / mart / ev3 / ex1 / nl / ru / des / WavPlayer.java
1 package nl.ru.des;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 import lejos.hardware.Audio;
10 import lejos.utility.Delay;
11
12 public class WavPlayer extends Thread{
13 private static String current;
14 private Audio audio = null;
15
16 public static void playWav(String c){
17 current = c;
18 }
19
20 public WavPlayer(Audio audio){
21 this.audio = audio;
22 audio.setVolume(Audio.VOL_MAX);
23 }
24
25 public void preLoad(String[] files){
26 LCDPrinter.print("Preloading audiofiles...");
27 for(String sound : files){
28 try {
29 LCDPrinter.print("Preloading: " + sound);
30 InputStream inp = new BufferedInputStream(Class.class.getResourceAsStream("/nl/ru/des/sounds/" + sound));
31 File f = new File(sound);
32 if(f.exists()){
33 inp.close();
34 LCDPrinter.print(sound + " already exists, skipping...");
35 continue;
36 }
37 FileOutputStream outp = new FileOutputStream(sound);
38 byte[] buffer = new byte[1024];
39 int length;
40 while ((length = inp.read(buffer)) > 0){
41 outp.write(buffer, 0, length);
42 }
43 outp.close();
44 LCDPrinter.print("Done...");
45 } catch (IOException e) {
46 e.printStackTrace();
47 }
48 }
49 }
50
51 @Override
52 public void run(){
53 File c;
54 while(true){
55 if(current != null){
56 c = new File(current);
57 if(c.canRead()){
58 audio.playSample(new File(current));
59 } else {
60 LCDPrinter.print("can't load: " + current);
61 }
62 current = null;
63 }
64 Delay.msDelay(00);
65 }
66 }
67 }