Question

I am using the CMU sphinx library to record sound. When i begin my java application, I allocate the Recognizer and the Configuration Manager only once as follows:

        cm = new ConfigurationManager(soundPart.class.getResource("hellongram.config.xml"));
        recognizer = (Recognizer) cm.lookup("recognizer");
        recognizer.allocate();

Moreover, I have a record sound button in my application. When the user clicks it, I use the below code to record sound :

    Microphone microphone = (Microphone)MR.sp.cm.lookup("microphone");
    if (!microphone.startRecording()) {
        System.out.println("Cannot start microphone.");
        MR.sp.recognizer.deallocate();

        System.exit(1);
    }
    //MR.sp.pleaseStartSpeaking.setVisible(true);
    while(true){
    Result result = MR.sp.recognizer.recognize();
    if(result!=null){
        String resultText = result.getBestFinalResultNoFiller();
        MR.sp.lblYouSearched.setVisible(true);
        MR.sp.lblNewLabel.setVisible(true);
        MR.sp.lblNewLabel.setText(resultText);
        MR.textQuery = resultText.toLowerCase();
        break;
    }
    }

This works the first time I do it. However, if the user clicks the record button the second time, it throws the error "Cannot start microphone". What I am doing wrong here. WHy can't I acquire the microphone the second time

Était-ce utile?

La solution

You might want to look into Microphone's RecordingThread here as well as re-read the page where you got that code here since the difference between the code you used and what you want to do is the code you used doesn't start and stop the microphone, it's continuously recording. A RecordingThread looks to be what you're after since you can easily call start() and stop() to get what you're after.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top