The s3eVideo API

We'll finish our look at Marmalade's multimedia support by having a whirlwind look at support for playing video clips using the s3eVideo API. To make use of the functions it provides, we just need to include the s3eVideo.h file into our source code.

Before we begin, there are two things to consider when using video clips in our games. The first is that while it is possible to specify where on the screen the video clip will appear, it will always be drawn on top of all other graphics. The second issue is that due to hardware limitations in many mobile devices, the s3eVideo API cannot be used at the same time as the s3eAudio and s3eSound APIs. In the case of s3eAudio, any currently playing track will be stopped (this also applies the other way around—starting an audio track will stop a currently playing video clip). The s3eSound API will continue processing its events while a video clip is playing, but its sound output will be silenced until the video clip is finished. For most games we would probably decide it is best to explicitly stop all s3eSound playback before starting a video clip, particularly if we are doing anything advanced like joining sound samples together using the callback system.

Starting video playback

The s3eVideo API works in a similar manner to the s3eAudio API. To start playing a video clip we use the s3eVideoPlay function, specifying the filename of the video clip, the number of times we want it to loop, a screen position, and the size that we want to display it at, as follows:

s3eVideoPlay(lFileName, lRepeatCount, lX, lY, lWidth, lHeight);

The video clip will automatically resize to fit the rectangle, but no attempt is made to keep the correct aspect ratio.

Where possible it is usually best to try to make your video clips the same resolution as the rectangular area you want to display them in. This will avoid any unnecessary stretching of the image (which can look quite ugly!) and may lead to slightly better performance, though on most modern devices the resize will be happening in hardware and there will be no appreciable difference.

The actual size of the video file itself is also worth bearing in mind, since we often want to minimize the size of the final install package. Ultimately, we need to use a bit of trial and error until we get a result that ticks all the boxes for acceptable quality, performance, and file size.

Determining video codec support

The s3eVideo API makes use of the device's built-in video decoding, so not all video formats will be playable on all devices. To determine whether support for a particular codec is available, there is a function called s3eVideoIsCodecSupported that takes a value from the s3eVideoCodec enum. Take a look at the s3eVideo.h file or the Marmalade documentation for a complete list of possible values.

Pausing, resuming, and stopping video playback

Again the parallels with the s3eAudio API are apparent when it comes to controlling video playback. The functions s3eVideoPause, s3eVideoResume, and s3eVideoStop all take no parameters and are used to pause, resume, and finish video clip playback respectively.

End of video notification

We have the choice of polling or callbacks once more for detecting the end of video playback. Let's start with the polled method that involves a call to the function s3eVideoIsPlaying, which will return S3E_TRUE if a video is playing or S3E_FALSE if a video is paused or stopped. Quite simple really!

If we want to use the callback approach, the following code snippet illustrates what to do:

int32 VideoFinished(void* apSystemData, void* apUserData)
{
  // apSystemData will always be NULL as there is no data associated
  // with this callback.
  // Return value is unimportant.
  return 0;
}

// To set up the callback function
s3eVideoRegister(S3E_VIDEO_STOP, (s3eCallback) VideoFinished, NULL);

// And to cancel it again...
s3eVideoUnRegister(S3E_VIDEO_STOP, (s3eCallback) VideoFinished);

The callback will be triggered whenever video playback stops, either because we explicitly call s3eVideoStop, an error in playback such as a corrupted video file occurs, or if an audio track is started using s3eAudioPlay. Note that the callback is not triggered between repetitions of the video clip if we are looping it.

For most games, video clips will probably only be used during introductory sequences or tutorials, since using video in the game itself is probably not practical. With this in mind, a polled approach for detecting when a video clip is finished is normally sufficient.

Other video queries

The s3eVideo API, like the s3eSound and s3eAudio APIs, also has a pair of functions for reading and writing global video parameters. They are called s3eVideoGetInt and s3eVideoSetInt. They are called as follows:

int32 lValue = s3eVideoGetInt(lProperty);
s3eVideoSetInt(lProperty, lValue);

The following table shows the values that can be used for the lProperty parameter:

Property

Description

S3E_VIDEO_VOLUME

This property is used to find the current volume level for the sound associated with the video clip and also to set a new volume. The maximum volume level is defined by the value S3E_VIDEO_MAX_VOLUME.

S3E_VIDEO_DEFAULT_VOLUME

This is a read-only property that shows the default volume that will be used for playing back the sound in a video clip. Its value is intended to provide a similar level of volume across all device types.

S3E_VIDEO_STATUS

This is a read-only parameter showing the current status of the video playback. It will return one of the following values: S3E_VIDEO_STOPPED, S3E_VIDEO_PLAYING, S3E_VIDEO_PAUSED, or S3E_VIDEO_FAILED.

S3E_VIDEO_POSITION

This property returns the current playback position of the video in milliseconds, or 0 if no video is playing. This parameter cannot be written to, so it is not possible to jump to a particular point in a video clip.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.131