Rev 229 | Go to most recent revision | Details | 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 | |||
27 | int act_player_state=PLAYERSTATE_IDLE; |
||
28 | VolumeControl vc; |
||
29 | |||
30 | public MKStatusVoice(org.ligi.ufo.MKCommunicator _mk,DUBwiseCanvas _canvas) |
||
31 | { |
||
32 | canvas=_canvas; |
||
33 | mk=_mk; |
||
34 | new Thread( this ).start(); // fire up main Thread |
||
35 | } |
||
36 | |||
37 | public void playerUpdate(Player player, String event, Object data) |
||
38 | { |
||
39 | if(event == PlayerListener.END_OF_MEDIA) { |
||
40 | try { |
||
41 | defplayer(); |
||
42 | } |
||
43 | catch(MediaException me) { } |
||
44 | act_player_state=PLAYERSTATE_FIN; |
||
45 | player=null; |
||
46 | |||
47 | } |
||
48 | } |
||
49 | |||
50 | void defplayer() throws MediaException { |
||
51 | if (player != null) { |
||
52 | if(player.getState() == Player.STARTED) { |
||
53 | player.stop(); |
||
54 | } |
||
55 | if(player.getState() == Player.PREFETCHED) { |
||
56 | player.deallocate(); |
||
57 | } |
||
58 | if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) { |
||
59 | player.close(); |
||
60 | } |
||
61 | } |
||
62 | player = null; |
||
63 | } |
||
64 | |||
65 | |||
66 | public void start_playing(String name) |
||
67 | { |
||
68 | try { |
||
69 | |||
70 | try { |
||
71 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3"); |
||
72 | } |
||
73 | catch (Exception e) { |
||
74 | player = Manager.createPlayer(getClass().getResourceAsStream(name+".wav"), "audio/x-wav"); |
||
75 | } |
||
76 | player.addPlayerListener(this); |
||
77 | player.realize(); |
||
78 | act_player_state=PLAYERSTATE_PLAYING; |
||
79 | |||
80 | vc = (VolumeControl) player.getControl("VolumeControl"); |
||
81 | if(vc != null) { |
||
82 | vc.setLevel(100); |
||
83 | } |
||
84 | |||
85 | player.prefetch(); |
||
86 | player.setLoopCount(1); |
||
87 | player.start(); |
||
88 | } |
||
89 | catch (Exception e) { |
||
90 | |||
91 | } |
||
92 | } |
||
93 | |||
94 | public void wait_for_end() |
||
95 | { |
||
96 | while (act_player_state!=PLAYERSTATE_FIN) |
||
97 | { |
||
98 | try { Thread.sleep(5); } |
||
99 | catch (Exception e) { } |
||
100 | } |
||
101 | |||
102 | |||
103 | } |
||
104 | |||
105 | int info_from_debug_set=-1; |
||
106 | public void run() |
||
107 | { |
||
108 | while(true) |
||
109 | { |
||
110 | if (mk.connected&&(canvas.settings.do_sound)&&(mk.debug_data.UBatt()!=-1)&&(!mk.force_disconnect)) |
||
111 | { |
||
112 | int ubatt=mk.debug_data.UBatt(); |
||
113 | |||
114 | if (info_from_debug_set!=mk.stats.debug_data_count) |
||
115 | { |
||
116 | info_from_debug_set=mk.stats.debug_data_count; |
||
117 | start_playing(""+(ubatt/10)); |
||
118 | wait_for_end(); |
||
119 | |||
120 | if((ubatt%10)!=0) |
||
121 | { |
||
122 | start_playing("komma"); |
||
123 | wait_for_end(); |
||
124 | start_playing(""+(ubatt%10)); |
||
125 | wait_for_end(); |
||
126 | } |
||
127 | start_playing("volt"); |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | try { Thread.sleep(5000); } |
||
133 | catch (Exception e) { } |
||
134 | |||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | } |