Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

VideoRecordingDemo - Illegal State Exception

I took the source code from the VideoRecordingDemo,

 

http://docs.blackberry.com/en/developers/deliverables/17968/CS_Record_a_video_to_file_in_a_BB_device...

 

and created a test project for the 9650, simulator verison Verizon-6.0.0.431 and actual phone 9330 version 6.0 bundle 2333. With each of them, I get an illegal state exception for "UI engine accessed without holding the event lock". The error occurs at the "add(videoField)" line. 

 

From the error message, I assume I need to have the "add(videoField)" line in the event thread. I tried putting an invokeLater() before the try in the run() function, but then the "_player.addPlayerListener(this)" line throws an error. Any ideas on what I need to do to get around this?

 

Thanks in advance for any help.

 

private class VideoRecorderThread extends Thread implements    javax.microedition.media.PlayerListener
  {
    private Player _player;
    private RecordControl _recordControl;

    VideoRecorderThread() {}

    public void run(){
      try {
        _player = javax.microedition.media.Manager.createPlayer  
          ("capture://video?encoding=video/3gpp");

        _player.addPlayerListener(this);
        _player.realize();
        VideoControl videoControl = (VideoControl) _player.getControl 
          ("VideoControl");
        _recordControl = (RecordControl) _player.getControl
          ( "RecordControl" ); 

        Field videoField = (Field) videoControl.initDisplayMode
          (VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");

        try {
          videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
        }
        catch( MediaException me ) {
          // setDisplaySize is not supported
        }
                 
        add(videoField);

        _recordControl.setRecordLocation("file:///store/home/user/VideoRecordingTest.3gpp" );   
        _recordControl.startRecord(); 
        _player.start();

 

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,444
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: VideoRecordingDemo - Illegal State Exception

It looks like you have already found the solution for the fist exception, but I'll post this here for reference.

 

Update a screen on the Main Event Thread

 

What exceptioin is addPlayerListener throwing?

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

Re: VideoRecordingDemo - Illegal State Exception

I modified the code to get rid of the 'event lock' exception. The exception with the addPlayerListener was due to where I was trying to put the invokeLater(). Note that I should have included in the original post that I am using the BlackBerry 6.0 JDE running on Windows 7.

 

To get rid of the 'event lock' exception, I moved the declaration of videoField to the class level and added the invokeLater() immediately before the 'add(videoField)' line. I included the rest of the initial function inside the invokeLater() and added a screen invalidate. The code now looks as follows:

 

        private class VideoRecorderThread extends Thread 
          implements javax.microedition.media.PlayerListener {
    
            private Player _player;
            private RecordControl _recordControl;
            private Field videoField;
            
            VideoRecorderThread() {
            }
            
            public void run() {
                try {
                    _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
                    _player.addPlayerListener(this);
                    _player.realize();
                    VideoControl videoControl = (VideoControl)_player.getControl("VideoControl");
                    _recordControl = (RecordControl) _player.getControl("RecordControl" );
                     videoField = (Field)videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,"net.rim.device.api.ui.Field");
                    try {
                        videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
                    }
                    catch( MediaException me ) {
                        // setDisplaySize is not supported
                    }
                    
                    UiApplication.getUiApplication().invokeLater (new Runnable() {
                        public void run()
                        {   try {
                                add(videoField);
                                UiApplication.getUiApplication().getActiveScreen().invalidate();
                                _recordControl.setRecordLocation("file:///store/home/user/VideoRecordingTest.3gpp" );
                                _recordControl.startRecord();
                                _player.start();
                            }
                            catch( IOException e ) {
                                Dialog.alert(e.toString());
                            }
                            catch( MediaException e ) {
                                Dialog.alert(e.toString());
                            }
                        }
                    });                   
               }
                catch( IOException e ) {
                    Dialog.alert(e.toString());
                }
                catch( MediaException e ) {
                    Dialog.alert(e.toString());
                }
            }
            
            public void stop() {
                if (_player != null) {
                    _player.close();
                    _player = null;
                }
                
                if (_recordControl != null) {
                    _recordControl.stopRecord();
                    try {
                        _recordControl.commit();
                    }
                    catch (Exception e) {
                        Dialog.alert(e.toString());
                    }
                    _recordControl = null;
                }
            }    

 

With this, I now get a null pointer exception from  '_recordControl.commit()'. Looking at _recordControl in the debugger, I see that _stream and _vrt are null.  Any thoughts on this?

 

A couple of additional questions:

 

1) When I select 'Start Recording' in the simulator, all I see is a white screen with the title "video recoding demo" (on the actual 9330 that I have, I see the video screen). Is it possible to see video on the simulator and, if so, what do I need to do to see it?

 

2) I have taken this code is taken directly from the 6.0 Development Guide. I created a new project with the only file in the project being VideoRecordingDemo.java. Are you able to run that code on a simulator/device and have it work?

Please use plain text.
Administrator
MSohm
Posts: 14,444
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: VideoRecordingDemo - Illegal State Exception

The BlackBerry smartphone simulators can record video using a webcam.  Do you have one on your PC?  If there isn't one present there won't be anything to record/preview.

 

Are you getting the NullPointerException on the device and simulator?

 

Are you aware that the BlackBerry Java SDK comes with a sample application to record and play back video (videorecordingdemo)?  To add it to you project choose the File menu of Eclipse, Import, BlackBerry, Import BlackBerry Samples and then complete the import wizard.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

Re: VideoRecordingDemo - Illegal State Exception

I got a web cam (didn't have one before) and it works fine if I go to the video camera directly. Using the VideoRecordingDemo.java code, I still have a screen with a white background.  Based on what I am seeing in the JDE debugger, it appears that 'videoField' is undefined in the event thread and thus I am not actually adding anything to the screen. I say appears because the JDE says it is undefined, but I would have expected an exception when trying to add an undefined variable. To address that I tried moving the invokeLater() to earlier in the routine. Eventually, because of dependencies, you have to put it around the entire run() function, which causes a compilation error with addPlayerListener(this), as 'this' is now Runnable instead of VideoRecorderThread implementing PlayerListener.

 

The NullPointerException occurs with both the simulator and the actual phone.

 

I was unaware of the videorecordingdemo. I imported it into the JDE and tried it on the simulator. The video screen works with the web cam, but when I hit 'start recording', I got an access violation error followed quickly by a message queue full error. I downloaded the application to the 9330 and it ran fine.

Please use plain text.
Administrator
MSohm
Posts: 14,444
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: VideoRecordingDemo - Illegal State Exception

[ Edited ]

The entire run thread shouldn't be wrapped in an invokeLater.  That defeats the purpose of creating a new thread (since everything would be run on the main event thread anyway).

 

Did you configure support for the micro SD card in the BlackBerry smarpthone simulator?  It is required by this sample.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

Re: VideoRecordingDemo - Illegal State Exception

The micro SD card is configured.

 

I made some more changes to minimize the code put in the event thread.  I created a Runnable class and passed the videoField to it as a parameter and changed invokeLater to invokeAndWait so the code should now run in the same order as in the documentation.

 

With this, it appears the camera is not being intialized properly(?). When I start the program on the simulator, the entire background is white (with a 'Video recording demo' title). I then select 'Start Recording'. The screen changes in that there are now arrowheads at the right top and bottom, but the camera image is not shown. I then select 'Stop Recording' and get a NullPointerException that occurs with the '_recordControl.commit()' line. A VideoRecodingTest.3gpp file is created in the SDCard folder I have setup, but it is empty. When I look at the event log, there is a 'NullPointerException' that says:

 

'No detail message', "

'net_rim_bb_videorecorder(4D0CDBB2)'

'VideoRecordController$1''

'run'

'0x213F'

 

Prior to the commit, _recordControl contains the following values:

 

endTime  25540000L

startTime 25540000L

_playerState 300 (note it was 400, but the player is closed before the commit occurs)

_state 1

_stream null

_viewfinderDisplayed true

_viewfinderSTate -1

_vrt null

 

 

On the actual phone, when I select 'Start Recording' I do see the camera image. When I select 'Stop Recording' the application hangs. A VideoRecordingTest.3gpp file is created but it again has no content.  The event log contains a very similar NullPointerException as to above.

 

The code I am using now up to the '_recordControl.commit()' is as follows:

 

        private class VideoRecorderThread extends Thread implements javax.microedition.media.PlayerListener {
            private Player _player;
            private RecordControl _recordControl;
            private Field videoField;
            private VideoControl _videoControl;
            
            VideoRecorderThread() {
            }
            
            private class RunnableTest implements Runnable {
                private Field tmpVideoField;
                
                public RunnableTest(Field argVideoField) {
                    tmpVideoField = argVideoField;
                }
                    
                public void run() {
                    add(tmpVideoField);
                }
            }
            public void run() {
                try {
                    _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp");
                    _player.addPlayerListener(this);
                    _player.realize();
                    _videoControl = (VideoControl)_player.getControl("VideoControl");
                    _recordControl = (RecordControl) _player.getControl("RecordControl" );
                    videoField = (Field)_videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,"net.rim.device.api.ui.Field");
                    try {
                        _videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
                    }
                    catch( MediaException me ) {
                        // setDisplaySize is not supported
                    }
                    
                    UiApplication.getUiApplication().invokeAndWait (new RunnableTest(videoField)); 
                    _recordControl.setRecordLocation("file:///SDCard/VideoRecordingTest.3gpp" );
                    _recordControl.startRecord();
                    _player.start();
               }
                catch( IOException e ) {
                    Dialog.alert(e.toString());
                }
                catch( MediaException e ) {
                    Dialog.alert(e.toString());
                }
            }
            
            public void stop() {
               if (_player != null) {
                    _player.close();
                    _player = null;
                }
                 if (_recordControl != null) {
                    _recordControl.stopRecord();
                    try {
                        _recordControl.commit();

 

Please use plain text.
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

Re: VideoRecordingDemo - Illegal State Exception

I started walking through the VideoRecordingDemo from the SDK to compare with the VideoRecordingDemo file from the developer's documentation.

 

The primary difference was that the SDK project did not create inner classes and spawn threads before creating the player  and setting up the video and record controls. I removed those classes and put everything up in the class that extends MainScreen. At that point, my webcam started to work with the simulator.

 

With these changes, the application works, with very limited testing, on the actual 9930 device. In the simulator, the application starts with the webcam is working. When I select 'Start Recoring' the video freezes, though the application is still responsive. However, if I select 'Stop Recording' the commit commands fails as before with a NullPointerException.

 

The code now looks as follows:

 

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.*;
import java.lang.*;
import javax.microedition.media.*;
import java.io.*;
import javax.microedition.media.control.*;
import net.rim.device.api.util.*;
import net.rim.device.api.command.*;

/**
 * 
 */
    class VideoRecordingDemoScreen extends MainScreen {
        private Player _player;
        private RecordControl _recordControl;
        private Field videoField;
        private VideoControl _videoControl;
        private MenuItem _startRecord;
        private MenuItem _stopRecord;

        public VideoRecordingDemoScreen() {
            setTitle("Video recording demo");
            
            _startRecord = new MenuItem(new StringProvider("Start recording"), 0x230060, 0);
            _startRecord.setCommand(new Command(new CommandHandler() 
            {
                public void execute(ReadOnlyCommandMetadata metadata, Object context) 
                {
                    startRecord();
                }
            }));
            
            addMenuItem(_startRecord);
            
            _stopRecord = new MenuItem(new StringProvider("Stop recording"), 0x230070, 0);
            _stopRecord.setCommand(new Command(new CommandHandler() 
            {
                public void execute(ReadOnlyCommandMetadata metadata, Object context) 
                {
                    stopRecord();
                }
            }));
            addMenuItem(_stopRecord);
            
            try {
                _player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/3gpp&mode=standard");
                _player.start();
                _videoControl = (VideoControl) _player.getControl("VideoControl");
                _recordControl = (RecordControl) _player.getControl("RecordControl");
                videoField = (Field) _videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,"net.rim.device.api.ui.Field");
                _videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
                add(videoField);
            }
            catch( IOException e ) {
                Dialog.alert(e.toString());
            }
            catch( MediaException me ) {
                // setDisplaySize is not supported
            }
        }
                            
        protected void onVisibilityChange(boolean visible)
        {
            // If this screen is visible and the video player exists,  
            // display the captured video.
            if( visible && _player != null )
            {
                _videoControl.setVisible(true);
            }
        }
    
        private void startRecord() {
            try {
            _recordControl.setRecordLocation("file:///SDCard/VideoRecordingTest.3gpp" );
            _recordControl.startRecord();
            }
            catch( IOException e )  {
                 Dialog.alert(e.toString());               
            }    
            catch( MediaException me ) {
                // setDisplaySize is not supported
            }
        }
            
        private void stopRecord() {
            if (_recordControl != null) {
                _recordControl.stopRecord();
                try {
                    _recordControl.commit();
                }
                catch (NullPointerException e) {
                    Dialog.alert(e.toString());
                }
                catch (Exception e) {
                    Dialog.alert(e.toString());
                }
            }        
        }          
    }

 

 

Please use plain text.
Administrator
MSohm
Posts: 14,444
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: VideoRecordingDemo - Illegal State Exception

Just to clarify, it works fine on the device but only fails in the simulator?  Have you tried more than one simulator?  It could also be a conflict with your webcam.  Video recording in the simulator isn't 100% reliable (can run into issues with codecs, formats, etc...) and is best tested on a real device.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Contributor
drbrundage
Posts: 21
Registered: ‎06-01-2011
My Device: Curve
My Carrier: Verizon

Re: VideoRecordingDemo - Illegal State Exception

Correct. I have run a number of tests on the actual device (9930) and haven't had any issues with the basic functionality of the webcam and recording video.

 

For the simulator, I have only tried the 9650 and it did not work well as mentioned above - video screen freezing on start recording and a null pointer exception during commit after stop recording.

Please use plain text.