Subversion Repositories Projects

Rev

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;
    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)
    {

        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 {
               
                        player = Manager.createPlayer(getClass().getResourceAsStream(name), "audio/mp3");
                        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&&(!mk.force_disconnect))
                    {
                        int ubatt=mk.debug_data.UBatt();

                        start_playing((ubatt/10)+".mp3");
                        wait_for_end();
                        start_playing("komma.mp3");
                        wait_for_end();
                        start_playing((ubatt%10)+".mp3");
                        wait_for_end();
                        start_playing("volt.mp3");
               
       
                    }

                try { Thread.sleep(5000); }
                catch (Exception e)  {   }
               
            }
    }
   

   
}