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
New Contributor
nhswork
Posts: 6
Registered: ‎05-10-2013
My Device: Z10

how to prevent backlight timing out, working with sample code

I'm currently working with the VideoRecordingDemo from JDE 5.0 sample. I found the recording length is limited to 2min, i.e. as soon as screen blackout, recording stops, and only resumes when I wake the screen with key press.

 

I read on the following page

http://supportforums.blackberry.com/t5/Java-Development/Enable-the-backlight-and-prevent-the-BlackBe...

that Backlight.enable will do the trick. However when I put it into code with Timer and Timertask class, nothing happens, I get no error running the app, but backlight still times out at 2min (the current setting in the BB9700 Simulator running OS 5.0)

 

Compile completed with the following warning:

"Warning!: inner class 'com.rim.samples.device.videorecordingdemo.VideoRecordingScreen$CountDown' should be declared static"

 

currently timer.schedule is delaying execution by 3 seconds, but if I get this to work. I can change it to schedule(..., 3000, 3000) to repeat the task every 3 seconds. I know this and this is not the issue. right now can't even get the backlight.enable to execute once.

 

Please let me know how to make this work, thanks!

 

/*
 * VideoRecordingScreen.java
 *
 * Copyright © 1998-2010 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package com.rim.samples.device.videorecordingdemo;

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.Display;

import javax.microedition.media.*;
import javax.microedition.media.control.*;

import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import net.rim.device.api.system.Backlight;
import java.util.*;

/**
 * This screen allows the user to record videos to a file or to a stream and
 * enables the user to open the VideoPlaybackScreen to play the recorded video.
 */
public class VideoRecordingScreen extends MainScreen
{ 
    private boolean _pendingCommit;    
    private boolean _committed;
    private boolean _recording;
    
    private String _videoFile;

    private Player _player;
    private VideoControl _videoControl;
    private RecordControl _recordControl;

    private Timer timer = new Timer();  

    private ObjectChoiceField _recordLocation;

    //private ByteArrayOutputStream _outStream; 
    

    /**
     * Constructs a screen to display and record the video being captured from
     * the device's camera.
     * 
     * @param encoding The non-null video encoding to be used when recording video to a file
     * @param fileSystem The file system to record the video file to, <code>null</code> if no file system was chosen
     * 
     * @throws NullPointerException Thrown if <code>encoding</code> or <code>filePath</code> is null.
     */
    public VideoRecordingScreen(String encoding, String filePath)
    {
        if (encoding == null)
        {
            throw new NullPointerException("Video encoding can not be null");
        }        
        if (filePath == null)
        {
            throw new NullPointerException("File path can not be null");
        }
        
        try
        {
            // Start capturing video from the camera            
            _player = javax.microedition.media.Manager.createPlayer("capture://video?" + encoding);
            _player.start();

            _videoControl = (VideoControl) _player.getControl("VideoControl");
            _recordControl = (RecordControl) _player.getControl("RecordControl");

            // Initialize the video display
            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);
             _videoFile = filePath;
            startRecord();
            timer.schedule(new CountDown(), 3000);
        }
        catch( Exception e )
        {
            // Dispose of the player if it was created
            if( _player != null )
            {
                _player.close();
            }
            _player = null;

            deleteAll();
            removeAllMenuItems();
            
            VideoRecordingDemo.errorDialog(e.toString());
        }   
    }
        
   private class CountDown extends TimerTask {
      public void run() {
         //UiApplication.getUiApplication().invokeLater(new Runnable() {
         Backlight.enable( true, 255 );
         //Dialog.alert("blabla");
         //});
      }
   }


    /**
     * Commits the current recording
     */
    private MenuItem _commit = new MenuItem("Save recording", 0, 0)
    {        
        public void run()
        {
            commitRecording();
        }
    };
        
    

    /**
     * Plays the recording
     */
    private MenuItem _playRecording = new MenuItem("Play recording", 0, 0)
    {        
        public void run()
        {
            // Create the playback screen from the chosen video source            
            VideoPlaybackScreen playbackScreen;
            playbackScreen = new VideoPlaybackScreen(_videoFile);

            // Hide the video feed since we cannot display video from the camera
            // and video from a file at the same time.
            _videoControl.setVisible(false);
            //_displayVisible = false;

            UiApplication.getUiApplication().pushScreen(playbackScreen);

        }
    };
    

    private MenuItem _startRecord = new MenuItem("Resume recording", 0, 0)
    {        
        public void run()
        {
            startRecord();
        }
    };
    
    
    private MenuItem _stopRecord = new MenuItem("Pause recording", 0, 0)
    {        
        public void run()
        {
            stopRecord();
        }
    };
    

    /**
     * @see net.rim.device.api.ui.Screen#onClose()
     */
    public boolean onClose()
    {
        // Stop capturing video from the camera
        if( _player != null )
        {
            commitRecording();
            _player.close();
        }

        return super.onClose();
    }
    

    /**
     * @see net.rim.device.api.ui.container.MainScreen#onSavePrompt()
     */
    protected boolean onSavePrompt()
    {
        // Suppress the save prompt
        return true; 
    }
    

    /**
     * Creates the menu based on the current recording state
     *
     * @see net.rim.device.api.ui.Screen#makeMenu(Menu, int)
     */
    protected void makeMenu(Menu menu, int instance)
    {
        super.makeMenu(menu, instance);
        
        if(_recording)
        {
            menu.add(_stopRecord);
        }
        else
        {
            menu.add(_startRecord);
        }      

        // If currently recording video, allow the user to commit
        // and reset the current recording.
        if( _pendingCommit )
        {
            menu.add(_commit);
        }
        if(_committed) 
        {
            // Commit is complete, allow playback of the recording
            menu.add(_playRecording);
        }
        
    }
    

    /**
     * @see net.rim.device.api.ui.Field#onVisibilityChange(boolean)
     */
    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);
            //_displayVisible = true;
        }
    }

    /**
     * Starts the recording of video from the camera
     */
    private void startRecord()
    {
        try
        {
            // If the recording is not pending a commit, then we need to set
            // the location to commit to      
            if( !_pendingCommit )
            {                
                _recordControl.setRecordLocation(_videoFile);
                _pendingCommit = true;
                _committed = false;
            }

            _recordControl.startRecord();
            _recording = true;                
        }
        catch( Exception e )
        {
            VideoRecordingDemo.errorDialog(e.toString());
        }
    }
    

    /**
     * Stops the recording of video from the camera
     */
    private void stopRecord()
    {
        try
        {
            _recordControl.stopRecord();            
            _recording = false;
            
        }
        catch( Exception e )
        {
            VideoRecordingDemo.errorDialog("RecordControl#stopRecord() threw " + e.toString());
        }
    }
      
    
    /**
     * Commits the current recording
     */
    private void commitRecording()
    {
        try
        {                
            _recordControl.commit();

            // Reset the recording settings                                           
            _pendingCommit = false;
            _committed = true;
            _recording = false;

            // Alert the user that the recording has been committed             
            Dialog.alert("Video Saved to Folder");
        }
        catch( Exception e )
        {
            VideoRecordingDemo.errorDialog("RecordControl#commit() threw " + e.toString());
        }        
    } 
}