Rev 81 |
Blame |
Last modification |
View Log
| RSS feed
/**************************************
*
* Voice output for MK
*
* Author: Marcus -LiGi- Bueschleb
*
* see README for further Infos
*
*
**************************************/
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.util.*;
import java.io.*;
public class MKStatusVoice
implements Runnable,PlayerListener
{
MKCommunicator mk=
null;
DUBwiseCanvas canvas=
null;
Player player
;
public final int PLAYERSTATE_IDLE=
0;
public final int PLAYERSTATE_PLAYING=
1;
public final int PLAYERSTATE_FIN=
2;
int act_player_state=PLAYERSTATE_IDLE
;
VolumeControl vc
;
public MKStatusVoice
(MKCommunicator _mk,DUBwiseCanvas _canvas
)
{
canvas=_canvas
;
mk=_mk
;
new Thread( this ).
start(); // fire up main Thread
}
public void playerUpdate
(Player player,
String event,
Object data
)
{
if(event == PlayerListener.
END_OF_MEDIA) {
try {
defplayer
();
}
catch(MediaException me
) { }
act_player_state=PLAYERSTATE_FIN
;
player=
null;
}
}
void defplayer
() throws MediaException
{
if (player
!=
null) {
if(player.
getState() == Player.
STARTED) {
player.
stop();
}
if(player.
getState() == Player.
PREFETCHED) {
player.
deallocate();
}
if(player.
getState() == Player.
REALIZED || player.
getState() == Player.
UNREALIZED) {
player.
close();
}
}
player =
null;
}
public void start_playing
(String name
)
{
try {
try {
player = Manager.
createPlayer(getClass
().
getResourceAsStream(name+
".mp3"),
"audio/mp3");
}
catch (Exception e
) {
player = Manager.
createPlayer(getClass
().
getResourceAsStream(name+
".wav"),
"audio/x-wav");
}
player.
addPlayerListener(this);
player.
realize();
act_player_state=PLAYERSTATE_PLAYING
;
vc =
(VolumeControl
) player.
getControl("VolumeControl");
if(vc
!=
null) {
vc.
setLevel(100);
}
player.
prefetch();
player.
setLoopCount(1);
player.
start();
}
catch (Exception e
) {
}
}
public void wait_for_end
()
{
while (act_player_state
!=PLAYERSTATE_FIN
)
{
try { Thread.
sleep(5); }
catch (Exception e
) { }
}
}
public void run
()
{
while(true)
{
if (mk.
connected&&(canvas.
do_sound)&&(mk.
debug_data.
UBatt()!=-
1)&&(!mk.
force_disconnect))
{
int ubatt=mk.
debug_data.
UBatt();
start_playing
(""+
(ubatt/
10));
wait_for_end
();
if((ubatt
%10
)!=
0)
{
start_playing
("komma");
wait_for_end
();
start_playing
(""+
(ubatt
%10
));
wait_for_end
();
}
start_playing
("volt");
}
try { Thread.
sleep(5000); }
catch (Exception e
) { }
}
}
}