Creating a YouTube viewer with the BlackBerry 6 Communication API


The Communication and Parsing API introduced in the BlackBerry® 6 OS probably doesn’t get as much attention as it really deserves. It has one of those names that could mean just about anything, but when I created a sample with it, I found it made many tasks simple – and I barely scratched the surface of what was possible.

You’ll find the API specification in the package, where you will see the significant set of interfaces and classes that provide all this functionality. The API breaks up communication into a few parts: The Destination is the target for a connection, usually a URL, and you can send and receive Message objects through it. Destinations can block the execution thread, or not, and you can send messages without handling the response with a FireAndForgetDestination. A Destination can also be connected with a MessageProcessor, which will parse the messages for you. Several processors are included in the API for common message formats like XML, JSON, RSS, and so on. Processors can be chained to support even more functionality. Finally, Messages can be retrieved as MessageFutures, and Destinations can be used to receive pushes and other incoming connections.

Behind the scenes is even more great stuff, like message queuing, prioritization, and even support for inter-process communication. Before we get too buried in the details, I should point out that the BlackBerry 6 SDK includes a full sample application that shows how to do all of this. The sample I want to share with you today is an example using this new API to streamline development of a YouTube® client. With the proliferation of APIs like YouTube’s Data API, which offer ATOM and JSON-C (for more bandwidth saving) interfaces, many opportunities become quite accessible.

Full sample code is included so you can check it out, but let’s look at some of the Communication API in action. The destination is the YouTube Data server with several basic query parameters:

BlockingSenderDestination destination = DestinationFactory.createBlockingSenderDestination(_context,
URI.create(buildQueryURL(query, index, max)), _processor);

Where my URL is built from a base, and I’m just appending the search query string and variables to return a particular section of the feed, I end up with a URL like so:


The query I’ve used restricts the response somewhat to a mobile-friendly version, including filtering out any videos that don’t have a mobile video link – a useful feature for mobile clients that could be expanded on in this sample. I’ve also included a MessageProcessor to handle the output; in this case, it’s an XMLHashtableMessageProcessor, which nicely converts the ATOM response into a simple Hashtable, so I can do things like pulling all the titles out into a String array:

String[] titles = contents.getStringArray("/feed/entry/title");

Once I’ve grabbed all the details I care about, I can send the results back to the screen for displaying. However, the feed response contains a URL for the thumbnails, so I need to download that as well. This is a neat opportunity to use non-blocking destinations and update the display when the thumbnails arrive:

NonBlockingSenderDestination destination = DestinationFactory.createNonBlockingSenderDestination(_context,
					URI.create(url), this);

Those requests are fired off through the API and I’ve got a MessageListener waiting for them, which will take the data and inform the screen it can update the image from the placeholder:

public void onMessage(Destination destination, Message message) {
if (message instanceof ByteMessage) {
		byte[] results = ((ByteMessage) message).getBytePayload();
		EncodedImage image = EncodedImage.createEncodedImage(results, 0, -1);
		... //send the image to the Screen

Give the sample a look and check out the API in more depth to see how you can put it to work for you. We’ll follow this up later with a more detailed sample showcasing the advanced features of both YouTube’s Data API and the BlackBerry® Communication API.

Join the conversation

Show comments Hide comments
+ -
  • Tutorial on Creating a YouTube Viewer App for BlackBerry 6 - BerryReview - Mobile Edition

    […] are interested you can check out how they built the YouTube viewer for BlackBerry 6 devices in the tutorial at this link. | Older: Registration Opens for FIRST BlackBerry World Conference » This entry was posted […]

  • homer

    where is the sample download?

  • Ronen Halevy

    Hi Tim. So where exactly is this sample application? Cannot seem to find it anywhere…

  • Sibusiso

    I want/prefer to see youtube videos in my blackberry curve 8520.. Is it possible?

  • How to make a YouTube viewer using the BlackBerry 6 API | PocketBerry

    […] Check out the full tutorial >> […]

  • Gabor Berenyi

    I have a problem with the communications api — the amount of threads seems to be limited on the device, so after starting up a few downloads, onMessage() in the MessageListener will never be called. Are the messages not being queued?

  • Gabor Berenyi

    Another question: how do you use BIS-B with this new network API? I have an example code from RIM from 4 years ago that uses the old API.

blog comments powered by Disqus