2
votes

I am trying the captureAudio example given in the codenameone documentation https://gist.githubusercontent.com/codenameone/a347dc9dcadaa759d0cb/raw/089f171a37e43f558ce897a0b51cab46219c37c0/CaptureAudioSample.java

Copying the code here for convenience:

Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);

FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
    for(String file : fs.listFiles(recordingsDir)) {
        MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
        mb.addActionListener((e) -> {
            try {
                Media m = MediaManager.createMedia(recordingsDir + file, false);
                m.play();
            } catch(IOException err) {
                Log.e(err);
            }
        });
        hi.add(mb);
    }

    hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
        try {
            String file = Capture.captureAudio();
            if(file != null) {
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
                String fileName =sd.format(new Date());
                String filePath = recordingsDir + fileName;
                Util.copy(fs.openInputStream(file), fs.openOutputStream(filePath));
                MultiButton mb = new MultiButton(fileName);
                mb.addActionListener((e) -> {
                    try {
                        Media m = MediaManager.createMedia(filePath, false);
                        m.play();
                    } catch(IOException err) {
                        Log.e(err);
                    }
                });
                hi.add(mb);
                hi.revalidate();
            }
        } catch(IOException err) {
            Log.e(err);
        }
    });
} catch(IOException err) {
    Log.e(err);
}
hi.show();

I am using Intellij and trying to test the above code in device simulator. But when I click the mic button I see a 'file chooser' dialog box with only a cancel option enabled. On clicking cancel nothing happens. If I choose a wav file then click ok, it gets copied and i am able to play it in simulator. Is mic input not supported in simulator? Is it getting replaced with file input? Or am I doing anything wrong?

1

1 Answers

0
votes

We don't capture from the mic in the simulator as we consider the file chooser more convenient for debugging and simulating actual capture cases. This allows us to reproduce failures/test cases with 100% accuracy.

Also the media API's in JavaSE are "flaky" and we don't want to rely on them anymore than we have to.

On the device you will get a recorder interface as usual.

Notice that this is true for image, video capture and picking from the picture gallery too.