Subversion Repositories Projects

Rev

Rev 82 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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