Android中的线程

时间:2016-01-28 05:48:26

标签: java android multithreading

我写了一个应用程序,它从网上获取一些信息并使用媒体播放器播放。我的应用程序工作正常,但现在我需要用两种语言调用此信息,即以两种不同的语言重复相同的信息,一个接一个。 这是我的主题,它在用一种语言调用信息时工作得很好,但我想找到一种方法用其他语言调用相同的信息。任何帮助,将不胜感激。提前致谢。

public class QueueListenerThread extends Thread {

    private final String VOICE_DATA_FOLDER = "voicedata/";

    private final String RAW_VOICE_DATA_SCHEME = "android.resource://com.stech.QmaticVoiceModule/raw/voicedata";

    private final String CONTANTS_FOLDER = "constants/";

    private String BASE_LANGUAGE_FOLDER = "urdu/";

    private Queue<String> calledTickets = null;
    private Context context = null;
    private String[] q;


    public QueueListenerThread(String threadName, Context ctxt, Queue<String> reccalledTickets){
        super(threadName);

        context = ctxt;

        calledTickets = reccalledTickets;

    }

    @Override
    public void run() {

        //we have to keep looking for the new called tickets
        while (true){

            try{

                ListenQueue();

            }catch(Exception ex){

                ex.printStackTrace();

            }

        }

    }

    private void ListenQueue() throws InterruptedException{

        if (calledTickets.size() > 0){

            String recString = calledTickets.poll();

            StrTokenizer tokenizer = new StrTokenizer(recString, '-');
            List<String> tokens = tokenizer.getTokenList();

            q = voiceQueueForTicketCounter(tokens.get(0), tokens.get(1));

            MediaPlayerThread mpThread = new MediaPlayerThread("MediaPlayer Thread", context, q);
            mpThread.start();

            try {
                mpThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        //wait one second
        Thread.sleep(1000);

    }

    private String[] voiceQueueForTicketCounter(String Ticket, String Counter){

        List<String> q = new ArrayList<String>();

        if (Constants.IS_VOICE_DATA_ON_SD_CARD){



        }else{

            //access files from raw resource

            //add bell voice
            q.add(BASE_LANGUAGE_FOLDER + "bell.wav");


            //add Ticket voice
            q.add(BASE_LANGUAGE_FOLDER + "tn.wav");


            //remove all leading zeros from Ticket Number if found 001 - 099
            Ticket = Ticket.replaceFirst("^0+(?!$)", "");

            if (Ticket.length() <= 2){

                q.add(BASE_LANGUAGE_FOLDER + Ticket + ".wav");

            }
            else if (Ticket.length() == 3){

                q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(0, Ticket.length() - 2)) + "00.wav");

                if (!((Ticket.substring(1)).replaceFirst("^0+(?!$)", "")).equalsIgnoreCase("0")){

                    q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(1)).replaceFirst("^0+(?!$)", "") + ".wav");

                }

            }
            else if (Ticket.length() == 4){

                //first play thousand file for urdu
                if (BASE_LANGUAGE_FOLDER == "urdu/"){

                    q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(0, Ticket.length() - 3)) + "01.wav");

                }else if (BASE_LANGUAGE_FOLDER == "dari/"){

                    //call the single number
                    q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(0, Ticket.length() - 3)) + ".wav");

                    //call the 1000 voice
                    q.add(BASE_LANGUAGE_FOLDER + "1000.wav");

                }

                //then play hundred file
                Ticket = (Ticket.substring(1)).replaceFirst("^0+(?!$)", "");

                if (Ticket.length() == 3){

                    q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(0, Ticket.length() - 2)) + "00.wav");

                    if (!((Ticket.substring(1)).replaceFirst("^0+(?!$)", "")).equalsIgnoreCase("0")){

                        q.add(BASE_LANGUAGE_FOLDER + (Ticket.substring(1)).replaceFirst("^0+(?!$)", "") + ".wav");

                    }

                }
                else if (Ticket.length() <= 2){

                    q.add(BASE_LANGUAGE_FOLDER + Ticket + ".wav");
                }

            }

            //add counter voice
            q.add(BASE_LANGUAGE_FOLDER + "counter.wav");

            //add counter 
            q.add(BASE_LANGUAGE_FOLDER + Counter + ".wav");

            //add per tashreef lay jaiye
            q.add(BASE_LANGUAGE_FOLDER + "ptlj.wav");

        }


        return q.toArray(new String[0]);
    }

}

0 个答案:

没有答案