Rev 206 | Rev 265 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
206 | ligi | 1 | /****************************************** |
2 | * |
||
3 | * Voice output for MK |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | *******************************************/ |
||
8 | |||
9 | import javax.microedition.media.*; |
||
10 | import javax.microedition.media.control.*; |
||
11 | import java.util.*; |
||
12 | import java.io.*; |
||
13 | |||
14 | |||
15 | public class MKStatusVoice |
||
16 | implements Runnable,PlayerListener |
||
17 | { |
||
18 | |||
19 | org.ligi.ufo.MKCommunicator mk=null; |
||
20 | DUBwiseCanvas canvas=null; |
||
21 | Player player; |
||
22 | |||
23 | public final int PLAYERSTATE_IDLE=0; |
||
24 | public final int PLAYERSTATE_PLAYING=1; |
||
25 | public final int PLAYERSTATE_FIN=2; |
||
26 | |||
229 | ligi | 27 | |
28 | public int volume=100; |
||
29 | public int delay=5; |
||
30 | |||
206 | ligi | 31 | int act_player_state=PLAYERSTATE_IDLE; |
32 | VolumeControl vc; |
||
33 | |||
34 | public MKStatusVoice(org.ligi.ufo.MKCommunicator _mk,DUBwiseCanvas _canvas) |
||
35 | { |
||
36 | canvas=_canvas; |
||
37 | mk=_mk; |
||
38 | new Thread( this ).start(); // fire up main Thread |
||
39 | } |
||
40 | |||
41 | public void playerUpdate(Player player, String event, Object data) |
||
42 | { |
||
43 | if(event == PlayerListener.END_OF_MEDIA) { |
||
44 | try { |
||
45 | defplayer(); |
||
46 | } |
||
47 | catch(MediaException me) { } |
||
48 | act_player_state=PLAYERSTATE_FIN; |
||
49 | player=null; |
||
50 | |||
51 | } |
||
52 | } |
||
53 | |||
54 | void defplayer() throws MediaException { |
||
55 | if (player != null) { |
||
56 | if(player.getState() == Player.STARTED) { |
||
57 | player.stop(); |
||
58 | } |
||
59 | if(player.getState() == Player.PREFETCHED) { |
||
60 | player.deallocate(); |
||
61 | } |
||
62 | if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) { |
||
63 | player.close(); |
||
64 | } |
||
65 | } |
||
66 | player = null; |
||
67 | } |
||
68 | |||
69 | |||
70 | public void start_playing(String name) |
||
71 | { |
||
72 | try { |
||
73 | |||
74 | try { |
||
75 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3"); |
||
76 | } |
||
77 | catch (Exception e) { |
||
78 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav"); |
||
79 | } |
||
80 | player.addPlayerListener(this); |
||
81 | player.realize(); |
||
82 | act_player_state=PLAYERSTATE_PLAYING; |
||
83 | |||
84 | vc = (VolumeControl) player.getControl("VolumeControl"); |
||
85 | if(vc != null) { |
||
229 | ligi | 86 | vc.setLevel(volume); |
206 | ligi | 87 | } |
88 | |||
89 | player.prefetch(); |
||
90 | player.setLoopCount(1); |
||
91 | player.start(); |
||
92 | } |
||
93 | catch (Exception e) { |
||
94 | |||
95 | } |
||
96 | } |
||
97 | |||
98 | public void wait_for_end() |
||
99 | { |
||
100 | while (act_player_state!=PLAYERSTATE_FIN) |
||
101 | { |
||
102 | try { Thread.sleep(5); } |
||
103 | catch (Exception e) { } |
||
104 | } |
||
105 | |||
106 | |||
107 | } |
||
108 | |||
109 | int info_from_debug_set=-1; |
||
110 | public void run() |
||
111 | { |
||
112 | while(true) |
||
113 | { |
||
114 | if (mk.connected&&(canvas.settings.do_sound)&&(mk.debug_data.UBatt()!=-1)&&(!mk.force_disconnect)) |
||
115 | { |
||
116 | int ubatt=mk.debug_data.UBatt(); |
||
117 | |||
118 | if (info_from_debug_set!=mk.stats.debug_data_count) |
||
119 | { |
||
120 | info_from_debug_set=mk.stats.debug_data_count; |
||
121 | start_playing(""+(ubatt/10)); |
||
122 | wait_for_end(); |
||
123 | |||
124 | if((ubatt%10)!=0) |
||
125 | { |
||
126 | start_playing("komma"); |
||
127 | wait_for_end(); |
||
128 | start_playing(""+(ubatt%10)); |
||
129 | wait_for_end(); |
||
130 | } |
||
131 | start_playing("volt"); |
||
132 | } |
||
133 | |||
134 | } |
||
135 | |||
229 | ligi | 136 | try { |
137 | if (delay<1) |
||
138 | Thread.sleep(1000); |
||
139 | else |
||
140 | Thread.sleep(delay*1000); |
||
141 | } |
||
206 | ligi | 142 | catch (Exception e) { } |
143 | |||
144 | } |
||
145 | } |
||
146 | |||
147 | |||
148 | |||
149 | } |