Rev 269 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/******************************************
*
* Voice output for MK
*
* Author: Marcus -LiGi- Bueschleb
*
*******************************************/
//public class MKStatusVoice {}
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.util.*;
import java.io.*;
public class MKStatusVoice
implements Runnable,PlayerListener
{
DUBwiseCanvas canvas=
null;
Player player
;
Player up_player
;
Player down_player
;
public final int PLAYERSTATE_IDLE=
0;
public final int PLAYERSTATE_PLAYING=
1;
public final int PLAYERSTATE_FIN=
2;
public int volume=
100;
public int delay=
5;
int act_player_state=PLAYERSTATE_IDLE
;
VolumeControl vc
;
public MKStatusVoice
(DUBwiseCanvas _canvas
)
{
canvas=_canvas
;
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 Player init_player
(String name
)
{
Player _player=
null;
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();
vc =
(VolumeControl
) _player.
getControl("VolumeControl");
if(vc
!=
null) {
vc.
setLevel(volume
);
}
_player.
prefetch();
_player.
setLoopCount(1);
_player.
start();
}
catch (Exception e
) {
}
return _player
;
}
public void play_up
()
{
try {
act_player_state=PLAYERSTATE_PLAYING
;
if (up_player==
null)
up_player=init_player
("up");
up_player.
start();
}
catch (Exception e
) {
}
}
public void play_down
()
{
try {
act_player_state=PLAYERSTATE_PLAYING
;
if (down_player==
null)
down_player=init_player
("down");
down_player.
start();
}
catch (Exception e
) {
}
}
public void start_playing
(String name
)
{
try {
act_player_state=PLAYERSTATE_PLAYING
;
player=init_player
(name
);
player.
start();
}
catch (Exception e
) {
}
}
public void wait_for_end
()
{
while (act_player_state
!=PLAYERSTATE_FIN
)
{
try { Thread.
sleep(5); }
catch (Exception e
) { }
}
}
int info_from_debug_set=-
1;
int volt_timeout=
0;
public int BASE_SLEEP=
10;
public int last_alt=-
1;
public int loop_cnt
;
public int volts_play_cnt
;
public void run
()
{
while(true)
{
loop_cnt++
;
if ((canvas.
mk!=
null)&&(canvas.
mk.
connected)&&(canvas.
settings.
do_sound)&&(canvas.
mk.
UBatt()!=-
1)&&(!canvas.
mk.
force_disconnect))
{
volt_timeout--
;
if (info_from_debug_set
!=canvas.
mk.
stats.
debug_data_count)
{ // only when newdata
if (canvas.
settings.
do_volts_sound&&(volt_timeout
<0))
{
volts_play_cnt++
;
volt_timeout=
(delay
*1000)/BASE_SLEEP
;
int ubatt=canvas.
mk.
UBatt();
info_from_debug_set=canvas.
mk.
stats.
debug_data_count;
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");
wait_for_end
();
}
if (canvas.
settings.
do_altimeter_sound)
{
if (last_alt==-
1) last_alt=canvas.
mk.
Alt();
if (last_alt
>canvas.
mk.
Alt()+canvas.
settings.
altsteps)
{
// start_playing("down");
play_down
();
wait_for_end
();
last_alt-=canvas.
settings.
altsteps;
}
if (last_alt
<canvas.
mk.
Alt()-canvas.
settings.
altsteps)
{
//start_playing("up");
play_up
();
wait_for_end
();
last_alt+=canvas.
settings.
altsteps;
}
}
else
last_alt=-
1;
}
}
try {
Thread.
sleep(BASE_SLEEP
);
//if (delay<1)
// Thread.sleep(1000);
//else
// Thread.sleep(delay*1000);
}
catch (Exception e
) { }
}
}
}