Question

I am using the following code to try to capture video with codenameone 2.0

        tProperty.setHint("name the property that is a media");
        final CheckBox cbVideo = new CheckBox("Video");
        final Button bCapture = new Button("Capture Media");
        final MediaPlayer mpPlayer = new MediaPlayer();
        bCapture.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ect){
                try {
                    if (cbVideo.isSelected()) {
                        String value = Capture.captureVideo();
                        mpPlayer.setDataSource(value);
                        mpPlayer.setName(tProperty.getText());
                    }else {
                        String value = Capture.captureAudio();
                        mpPlayer.setDataSource(value);
                        mpPlayer.setName(tProperty.getText());

                    }
                }catch (Exception e){

                }
            }
        });
        cM.addComponent(tProperty);
        cM.addComponent(cbVideo);
        cM.addComponent(bCapture);
        cM.addComponent(mpPlayer);
        Command [] cmds = new Command[1];
        cmds[0] = new Command("Done") {
            public void actionPerformed(ActionEvent evt) {
                //do Option1
            }        
        };
           Dialog.show(editType, cM, cmds);            

When running in the simulator, clicking on the CaptureMedia button, it will present the file chooser interface. But then I am unable to choose any file at all whether audio or video because the choose file button is diabled. How do I get to test the video capture in the simulator?

Was it helpful?

Solution

I think it's a layout problem, you are adding the MediaPlayer component before the video was created, so it's preferred size is 0. Try to place the video in the border layout center so it's preferred size is ignored and the player will have enough space to display.

Try this:

    final Form hi = new Form("Hi World");
    hi.setLayout(new BorderLayout());

    final Button bCapture = new Button("Capture Media");
    bCapture.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ect) {
            try {
                final MediaPlayer mpPlayer = new MediaPlayer();
                String value = Capture.captureVideo();
                System.out.println("Captured Video " + value);
                if (value != null) {
                    System.out.println("Playing Video");
                    InputStream is = FileSystemStorage.getInstance().openInputStream(value);
                    String strMime = "video/mp4";
                    System.out.println("Input Stream" + is.available());
                    mpPlayer.setName("bla");
                    mpPlayer.setDataSource(is, strMime, new Runnable() {
                        public void run() {
                            System.out.println("reset the clip for playback");
                        }
                    });
                    hi.addComponent(BorderLayout.CENTER, mpPlayer);
                    hi.revalidate();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    );
    hi.addComponent(BorderLayout.NORTH, bCapture);

    hi.show();

OTHER TIPS

There is a regression in playing local videos in the Codename One simulator although it should work on the device. The next update of Codename One will fix it but for now you can workaround it by playing from a stream which should work just fine.

Just use the FileSystemStorage class to get an InputStream to the video and invoke the appropriate playback code. Note that this is less efficient than the play via URL API so when the regression is fixed you should probably return to the URL based API.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top