Start your video

Now we're ready to start playing our video! Assuming the video is located somewhere on your BlackBerry PlayBook (it will be if you've bundled it in your application's package), we need to let mm-renderer know where it is and tell it to start playing. To do that, we need to make the output window visible by posting something to it. Here, we fill the render buffer with the color black and define a rectangle that bounds the area to change (in this case, the entire width and height of the window). This buffer is then made visible through the window by calling screen_post_window().

screen_buffer_t temp_buffer[1];
screen_get_window_property_pv(screen_window,
    SCREEN_PROPERTY_RENDER_BUFFERS, (void**)temp_buffer);

int fill_attributes[3] = {SCREEN_BLIT_COLOR, 0x0, SCREEN_BLIT_END};
screen_fill(screen_context, temp_buffer[0], fill_attributes);

screen_get_window_property_iv(screen_window, SCREEN_PROPERTY_SIZE, size);
int temp_rectangle[4] = {0,0,size[0],size[1]};
screen_post_window(screen_window, temp_buffer[0], 1, temp_rectangle, 0);

If you were to run the application at this point, you would get a nice, black screen:

Screen showing the black screen.

This might be very calming but there's a few more steps to get something really playing. We want to make sure that the automatic dimming of the display's backlight doesn't prevent the user from enjoying the video. To do this, we set the SCREEN_PROPERTY_IDLE_MODE to SCREEN_IDLE_MODE_KEEP_AWAKE. Note that you don't really need to do this for videos that are actively playing but it's something useful to know for your other applications.

int idle_mode = SCREEN_IDLE_MODE_KEEP_AWAKE;
screen_set_window_property_iv(screen_window,
    SCREEN_PROPERTY_IDLE_MODE, &idle_mode);

We then create the path that represents the location of the media file on your BlackBerry PlayBook. In this case, we use the location of the bundled application resource but it could be anywhere on your device.

char cwd[PATH_MAX];
char media_file[PATH_MAX];
getcwd(cwd,PATH_MAX);

snprintf(media_file, PATH_MAX, "file://%s/app/playbook/native/pb_sample.mp4", cwd);

Next, attach the media file to the mm-renderer context you've created and tell it the type of media you have. Since we're playing only one file, we use "track" but some other possible types are "playlist", "dvd", and so on.

if (mmr_input_attach(mmr_context, media_file, "track");

Now, we can start playing the video.

mmr_play(mmr_context);

Stop the video

Now that the video is playing, you probably want to allow the user to stop it. Before your application can do that, it needs the ability to listen for screen events and handle any user input. Once the user indicates that they've had enough of the video, the application can stop playback and close the window.

To allow the user to tell the application to stop playing, we need to setup an event handling loop and request events from the screen and the navigator:

screen_request_events(screen_context);
navigator_request_events(0);

for (;;) {
    bps_event_t *event = NULL;
    if (bps_get_event(&event, -1) != BPS_SUCCESS) {
    	return EXIT_FAILURE;
    }

    if (event) {
        if (bps_event_get_domain(event) == navigator_get_domain() &&
                bps_event_get_code(event) == NAVIGATOR_EXIT) {

                exit_application = 1;
            }
        if (exit_application) {
            break;
        }
    }
 }

We use -1 in the call to bps_get_event() to make it wait for an event. This prevents the loop from running fast and consuming processor cycles when it's not really necessary. Once the application gets an event, we look for kind of event that describes the user swiping up from the bezel and closing the application from the navigator (NAVIGATOR_EXIT). It would look something like this:

Screen showing the video playback window sample.

Once we know that the user wants to exit, we can go about stopping the video and cleaning up all the application artifacts we've created.

screen_stop_events(screen_context);

mmr_stop(mmr_context);

mmr_output_detach(mmr_context, audio_device_output_id);

mmr_output_detach(mmr_context, video_device_output_id);

mmr_context_destroy(mmr_context);

mmr_disconnect(mmr_connection);

bps_shutdown();

screen_destroy_window(screen_window);

screen_destroy_context(screen_context);

That's it, you can now play a video! You can try altering the application to use different videos or perhaps add user interface elements to control playback.