Subversion Repositories Projects

Rev

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