Best Practices for Android App Development

Product
Video Cloud
Applies to Roles
Developer
Version
Brightcove 5
Edition
Express 499, Pro, Enterprise

While working with the Video Cloud App SDK for Android, there are some potential pitfalls that you will want to avoid. This article will help keep you on the right track as you code your application. Also remember to review these other documents about the Video Cloud App SDK for Android:

Unit testing

Unit testing is a great way to make your applications more stable, and to see if your code does what you want it do. The Google Android SDK has JUnit support, as well as a convenient plug-in for the Eclipse IDE. 

Handling common error scenarios

You must enter an API token or the BCMediaAPI will throw an error when you try to make a call.  The token is defined via the ReadAPI class, which can then be used to call the desired API method.  For example, to call the findAllVideos() method:

private void makeAPICall() {
        ReadAPI api = new ReadAPI("TOKEN HERE");
        EnumSet<VideoFieldEnum> videoFields = VideoFieldEnum.createEmptyEnumSet(); 
        videoFields.add(VideoFieldEnum.ID);
        videoFields.add(VideoFieldEnum.NAME);
        
        try {
            ItemCollection<Video> collection = api.findAllVideos(5, 0, true,
                SortByTypeEnum.PUBLISH_DATE, SortOrderTypeEnum.ASC, videoFields, null);
            collection.getTotalCount();
            System.out.println(collection);
        } catch(Exception e) {
            String msg = e.getMessage();
        }
       
    } 

As in this example, it is wise to catch exceptions that may be returned from the actual BC Media API call. You may also want to check if the result is nil. If the object you are looking for is not found (for example, if you call findVideoById with an invalid video ID), the result will be nil and the error will contain a "null" message.

Handling unavailable network

Since your app is on the move with the user, network reliability can change at any time. If the user's device can't reach the Video Cloud service, the error pointer will be populated with an error that you should handle in a graceful way.

Playing invalid videos

If you try to play a video that is deactivated, invalid, inactive due to scheduling, of an unsupported file type (non-H.264), or in an unaccepted state (which may apply to videos shared with media sharing), the video loads a blank black screen in the player UI. You should avoid playing a video that is in any of these states.

Playing streaming videos

If you try to play a streaming video, the Android media player will throw an exception:  "Unknown error." The RTMP and RTMPe protocols are not supported by the Android media player. We suggest configuring your Video Cloud account to use Universal Delivery Service, so you can play your streaming content over HTTP. Once your account is using this service, you can use the setMediaDeliveryType() method in the ReadAPI class to cause all Media API calls to be made with the media_delivery=HTTP argument appended.

More Tips and Tricks

Notifications from other applications. Dismissing notifications from other applications that pop up during video playback will stop the playback without saving your place in the video.  This is a problem with the Android media player, and is common to all video applications on these devices.

Orientation. When the BCPlayer starts playing, you should lock the orientation of your view to landscape to provide the best user experience.

Hiding the status bar during playback. If you want to hide the status bar at the top of the Android screen during playback, you should include the following in the Activity that contains the BCPlayerView:

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	getWindow().setFlags(
			WindowManager.LayoutParams.FLAG_FULLSCREEN,
			WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setContentView(<R.layout.id_of_layout_containing_BCPlayerView>  );

	...

No geo-restriction. Even though the BCMediaAPI object will return your media's geo-restriction metadata, geo-restriction is not supported in the BCPlayer.

Region support. By default, all calls to the Video Cloud Media API go to api.brightcove.com, but you may elect to have them directed to api.brightcove.co.jp, instead.  To do this, use the setRegion() method in the ReadAPI class; the method takes a region enumerator value of either US or JP.

Encodings. Playing back videos with high resolutions can cause artifacts in the player. We recommend encoding low resolution renditions for mobile playback.  You may specify the range of acceptable encoding bit rates that you wish to playback in your application by setting those properties in the BCPlayer via the setRenditionBitRateRange() method in the BCPlayerView class.

Formats. Trying to play an unsupported video format — .flv and others — over HTTP will display an error alert.

Tags
Android, Droid, mobile