Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | package gdi1sokoban.sound; |
2 | |||
3 | import java.io.File; |
||
4 | import java.io.FileInputStream; |
||
5 | import java.io.IOException; |
||
6 | import java.net.URISyntaxException; |
||
7 | |||
8 | import javazoom.jl.decoder.JavaLayerException; |
||
9 | import javazoom.jl.player.Player; |
||
10 | |||
11 | /** |
||
12 | * Sound Entity class |
||
13 | * |
||
14 | * manages a thread which plays a predefined sound |
||
15 | * |
||
16 | */ |
||
17 | public class SoundEntity { |
||
18 | private volatile Player sndPlayer = null; |
||
19 | private volatile boolean play = false; |
||
20 | |||
21 | private Thread soundThread; |
||
22 | |||
23 | /** |
||
24 | * plays a sound |
||
25 | * @param fileName filename of the sound file |
||
26 | * @param sndOptions output options (SND_LOOP, SND_ASYNC, SND_SYNC - see SoundEngine for further details) |
||
27 | */ |
||
28 | protected void playSound(final String fileName, final int sndOptions) { |
||
29 | final boolean loop = ((sndOptions & SoundEngine.SND_LOOP) == SoundEngine.SND_LOOP); |
||
30 | final boolean async = ((sndOptions & SoundEngine.SND_ASYNC) == SoundEngine.SND_ASYNC); |
||
31 | play = true; |
||
32 | soundThread = new Thread() { |
||
33 | @Override |
||
34 | public void run() { |
||
35 | while (play) { |
||
36 | try { |
||
37 | FileInputStream sndStream; |
||
38 | sndStream = new FileInputStream(new File(ClassLoader.getSystemClassLoader().getResource(fileName).toURI())); |
||
39 | sndPlayer = new Player(sndStream); |
||
40 | sndPlayer.play(); |
||
41 | sndPlayer.close(); |
||
42 | } catch (final IOException e) { |
||
43 | } catch (final JavaLayerException e) { |
||
44 | break; |
||
45 | } catch (final URISyntaxException e1) { |
||
46 | } |
||
47 | if (loop == false) |
||
48 | play = false; |
||
49 | } |
||
50 | play = false; |
||
51 | if (sndPlayer != null) |
||
52 | sndPlayer.close(); |
||
53 | } |
||
54 | }; |
||
55 | if (async) |
||
56 | soundThread.start(); |
||
57 | else |
||
58 | soundThread.run(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * stops the sound |
||
63 | */ |
||
64 | protected void stopSound() { |
||
65 | play = false; |
||
66 | if (sndPlayer != null) |
||
67 | sndPlayer.close(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * checks whether the sound is still playing or not |
||
72 | * @return true if sound has already stopped playing, else false |
||
73 | */ |
||
74 | protected boolean hasStopped() { |
||
75 | return !play; |
||
76 | } |
||
77 | } |