Subversion Repositories Projects

Rev

Rev 70 | 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;
70 ligi 23
    MKMiniCanvas 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
 
70 ligi 33
    public MKStatusVoice(MKCommunicator _mk,MKMiniCanvas _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 {
74
                        player = Manager.createPlayer(getClass().getResourceAsStream(name+".mp3"), "audio/mp3");
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
 
95
 
96
 
97
        }      
31 ligi 98
    }
99
 
100
    public void wait_for_end()
101
    {
102
        while (act_player_state!=PLAYERSTATE_FIN)
103
            {
104
                try { Thread.sleep(5); }
105
                catch (Exception e)  {   }
106
            }
107
 
108
 
109
    }
110
 
111
    public void run()
112
    {
113
        while(true)
114
            {
115
 
116
 
70 ligi 117
                if (mk.connected&&(canvas.do_sound)&&(mk.debug_data.UBatt()!=-1)&&(!mk.force_disconnect))
31 ligi 118
                    {
119
                        int ubatt=mk.debug_data.UBatt();
120
 
77 ligi 121
                        start_playing(""+(ubatt/10));
31 ligi 122
                        wait_for_end();
59 ligi 123
 
124
                        if((ubatt%10)!=0)
125
                            {
77 ligi 126
                                start_playing("komma");
59 ligi 127
                                wait_for_end();
77 ligi 128
                                start_playing(""+(ubatt%10));
59 ligi 129
                                wait_for_end();
130
                            }
77 ligi 131
                        start_playing("volt");
31 ligi 132
 
133
 
134
                    }
135
 
136
                try { Thread.sleep(5000); }
137
                catch (Exception e)  {   }
138
 
139
            }
140
    }
141
 
142
 
143
 
144
}