# Titanium.Media.VideoPlayer

A native control for playing videos.

Availability
0.9
0.9
9.2.0

# Overview

The video player is a native view that can be used to play videos, either stored locally or streamed from a web server. The player can occupy the full screen, or can be used as a view that can be added to other views.

Use the Titanium.Media.createVideoPlayer method to create a video player.

All platforms support specifying the video content as a URL, either to a local file or a remote stream. This is done by setting the Titanium.Media.VideoPlayer.url property.

# iOS Implementation Notes

On iOS, video content can also be specified as a Titanium.Blob or Titanium.Filesystem.File object using the Titanium.Media.VideoPlayer.media property.

On iOS, a video player can dynamically switch back and forth between fullscreen mode and standard mode. If the native video controls are displayed, the user can use them to switch between standard and fullscreen mode.

# Android Implementation Notes

On Android, the video player cannot switch modes. To create a fullscreen player, you must specify fullscreen: true when you create the player. This fullscreen player creates its own Android Titanium.Android.Activity on top of the activity stack. Unlike a normal view, this fullscreen video player appears as soon as it is created. The user can close the player by pressing the Back button. See the code examples for a sample using the fullscreen player on Android.

There are several known issues with clipping on the Android video player.

  • When used with scalingMode set to VIDEO_SCALING_ASPECT_FILL or VIDEO_SCALING_NONE, the video content is not correctly clipped to the boundaries of the view. (TIMOB-7628 (opens new window))

  • When a border is set on the view, the video content does not take the border width into account, so the video content covers the border. (TIMOB-7628 (opens new window))

  • If a borderRadius is specified, the video content is not clipped to the rounded corners. (TIMOB-7629 (opens new window))

On Android, using a video player inside a scroll view is not recommended. When scrolling, black bars may appear over the video content.

This is because the video player is rendered on a special Android UI element called a SurfaceView. Android renders the SurfaceView behind the main window and punches a hole in the window to reveal the video. Because the content of the SurfaceView does not reside in the application window, the video content cannot be transformed (moved, scaled or rotated) with the window. This makes it difficult for the content to render properly inside a ScrollView.

# Examples

# Simple Video Player Example

The following code creates a simple video player to play a local video file.

var vidWin = Titanium.UI.createWindow({
    title: 'Video View Demo',
    backgroundColor: '#fff'
});

var videoPlayer = Titanium.Media.createVideoPlayer({
    top: 2,
    autoplay: true,
    backgroundColor: 'blue',
    height: 300,
    width: 300,
    mediaControlStyle: Titanium.Media.VIDEO_CONTROL_DEFAULT,
    scalingMode: Titanium.Media.VIDEO_SCALING_RESIZE_ASPECT
});

videoPlayer.url = 'movie.mp4';
vidWin.add(videoPlayer);
vidWin.open();

# Android Fullscreen Video Player

The Android fullscreen video player operates differently from other video players. The following example shows how to create, show, and close a fullscreen video player.

Note that in this example, a button is included to close the player, to demonstrate a method for dismissing the player programmatically. In practice, the user can always dismiss the player by using the Back button, so an on-screen control would not be required.

var win = Titanium.UI.createWindow({
    title: 'Test',
    backgroundColor: '#fff'
});

// Change to a valid URL
var contentURL = "http://www.example.com/stream.mp4";

var openButton = Ti.UI.createButton({
    title: "Start Video",
    top: "0dp",
    height: "40dp",
    left: "10dp",
    right: "10dp"
});

openButton.addEventListener('click', function() {
    var activeMovie = Titanium.Media.createVideoPlayer({
        url: contentURL,
        backgroundColor: 'blue',
        mediaControlStyle: Titanium.Media.VIDEO_CONTROL_DEFAULT,
        scalingMode: Titanium.Media.VIDEO_SCALING_RESIZE_ASPECT_FILL,
        fullscreen: true,
        autoplay: true
    });

    var closeButton = Ti.UI.createButton({
        title: "Exit Video",
        top: "0dp",
        height: "40dp",
        left: "10dp",
        right: "10dp"
    });

    closeButton.addEventListener('click', function() {
        activeMovie.hide();
        activeMovie.release();
        activeMovie = null;
    });

    activeMovie.add(closeButton);
});
win.add(openButton);
win.open();

# Alloy XML Markup

Previous simple example as an Alloy view.

simplevideoplayer.xml:

<Alloy>
    <Window id="vidWin" title="Video View Demo" backgroundColor="#fff">
        <VideoPlayer id="videoPlayer" ns="Ti.Media" top="2" url="/movie.mp4"
                      height="300" width="300" backgroundColor="blue" autoplay="true" />
    </Window>
</Alloy>

# Properties

# allowsAirPlay

Availability
0.9
9.2.0
allowsAirPlay :Boolean

Whether or not the current movie can be played on a remote device.


# autoplay

Availability
0.9
0.9
9.2.0
autoplay :Boolean

Indicates if a movie should automatically start playback.

On iOS, playback starts automatically if autoplay is true and the movie is likely to finish uninterrupted. The OS determines whether the movie is likely to finish uninterrupted based on factors including the bit rate of the movie and network conditions (if the movie is being streamed from a remote source).

Default: true


# backgroundView DEPRECATED

Availability
0.9
9.2.0
backgroundView :Titanium.UI.View

DEPRECATED SINCE 7.0.0

Use overlayView instead.

Sets the background view for customization which is always displayed behind movie content.

To create a background behind the movie content on iOS, you can specify a background view. On iOS, you can set the backgroundColor property directly on the video player, but not a background image or background gradient. For this kind of effect, specify a background view.

Note that the background view must be sized explicitly: percentage sizes do not work properly.

The following code sample creates a video player with a background image:

var videoPlayer = Ti.Media.createVideoPlayer({
    height: 300,
    width: 300,
    backgroundView: Ti.UI.createView({
        backgroundImage: 'videoPlayerBG.png
        width: 300,
        height: 300
    }),
    borderRadius: 20,
    borderWidth: 2,
    borderColor: 'blue',
});

Note that if scalingMode is set to VIDEO_SCALING_RESIZE or VIDEO_SCALING_RESIZE_ASPECT_FILL, the playing video will fill the entire video player area, obscuring any background view.


# currentPlaybackTime

Availability
0.9
0.9
9.2.0
currentPlaybackTime :Number

Current playback time of the current movie in milliseconds.


# duration

Availability
0.9
0.9
9.2.0
duration :Number

The duration of the current movie in milliseconds, or 0.0 if not known.

The duration of the movie may not be available when playback is started. The durationavailable event is fired when the duration is known.

Default: 0


# endPlaybackTime

Availability
0.9
0.9
9.2.0
endPlaybackTime :Number

The end time of movie playback, in milliseconds.

On iOS, defaults to 0, which indicates natural end time of the movie. Changing the value to a value less than duration causes the movie to stop playing at the specified point. On iOS, this value is not applicable to streaming media, so 0 is returned if the current media is being streamed.

On Android, this is always the same as duration (the natural end time), and changing the value has no effect.

Default: 0


# fullscreen CREATION ONLYDEPRECATED

Availability
0.9
fullscreen :Boolean

DEPRECATED SINCE 7.0.0

This property has been removed for iOS in Titanium SDK 7.0.0 as of the official deprecation by Apple.

Determines if the movie is presented in the entire screen (obscuring all other application content).

Note that this must be set at different times on different platforms:

On Android, this property must be set at creation time. For example:

var player = Ti.Media.createVideoPlayer({fullscreen: true});

On Android, setting this value to true means that the video will have its own Android Activity rather than being embedded as a view.

This property was removed on iOS in SDK 7.0.0

Default: false


# initialPlaybackTime

Availability
0.9
0.9
9.2.0
initialPlaybackTime :Number

The start time of movie playback, in milliseconds.

Default: 0


# loadState READONLYDEPRECATED

Availability
0.9
0.9
9.2.0
loadState :Number

DEPRECATED SINCE 7.0.0

On iOS, use moviePlayerStatus instead.

Returns the network load state of the movie player.


# media

Availability
0.9
9.2.0

Media object to play, as either a Ti.Filesystem.File, a Ti.Blob, or an URL String.


# mediaControlStyle DEPRECATED

Availability
0.9
mediaControlStyle :Number

DEPRECATED SINCE 7.0.0

Use showsControls instead.

The style of the playback controls.

Removed from iOS in SDK 7.0.0

Default: System default video controls (<Titanium.Media.VIDEO_CONTROL_DEFAULT>).


# moviePlayerStatus READONLY

Availability
0.9
9.2.0
moviePlayerStatus :Number

Returns the status of the movie player.


# naturalSize

Availability
0.9
9.2.0
naturalSize :MovieSize

Returns the natural size of the movie.

Returns a dictionary with properties width and height. Returns 0 for both properties if not known or applicable.

The naturalsizeavailable event is fired when the natural size is known.


# overlayView

Availability
0.9
9.2.0
overlayView :Titanium.UI.View

Use the overlay view to add additional custom views between the video content and the controls.

Note that the overlay view must be sized explicitly: percentage sizes do not work properly.

The following code sample creates a video player with a background image:

var videoPlayer = Ti.Media.createVideoPlayer({
    height : 300,
    width : 300,
    overlayView : Ti.UI.createView({
        backgroundImage: 'videoPlayerBG.png
        width : 300,
        height : 300
    }),
    borderRadius : 20,
    borderWidth : 2,
    borderColor : 'blue',
});

# pictureInPictureEnabled

Availability
0.9
9.2.0
pictureInPictureEnabled :Boolean

Whether or not the receiver allows Picture in Picture playback.

Default: true


# playableDuration READONLY

Availability
0.9
0.9
9.2.0
playableDuration :Number

Currently playable duration of the movie, in milliseconds, for progressively downloaded network content, or 0.0 if not known.

On Android, this is always the same as duration.

Default: 0


# playbackState READONLY

Availability
0.9
0.9
9.2.0
playbackState :Number

Current playback state of the video player.

One of the VIDEO_PLAYBACK_STATE constants defined in Titanium.Media.


# playing READONLY

Availability
0.9
0.9
9.2.0
playing :Boolean

Boolean to indicate if the player has started playing.


# repeatMode

Availability
6.2.0
0.9
9.2.0
repeatMode :Number

Determines how the movie player repeats when reaching the end of playback.

This API can be assigned the following constants:

Default: Titanium.Media.VIDEO_REPEAT_MODE_NONE


# scalingMode

Availability
0.9
0.9
9.2.0
scalingMode :Number

Determines how the content scales to fit the view.

Default: Titanium.Media.VIDEO_SCALING_RESIZE_ASPECT> on Android, <Titanium.Media.VIDEO_SCALING_RESIZE> on iOS


# showsControls

Availability
7.1.0
7.0.0
9.2.0
showsControls :Boolean

Whether or not the receiver shows playback controls. Default is true.

Clients can set this property to false when they don't want to have any playback controls on top of the visual content (e.g. for a game splash screen). This property should not be used to temporarily change the visibility of the playback controls since it will create or destroy UI elements.

Default: true


# url

Availability
0.9
0.9
9.2.0
url :String

URL of the media to play.

URL identifying a local or remote video to play.


# volume

Availability
2.1
9.2.0
volume :Number

Volume of the audio portion of the video.

On iOS, this adjusts the volume of the application's session as well, and will not work if useApplicationAudioSession has been set false. On the iOS device, setting this will be accompanied by an OS-provided indicator. However, the iOS simulator does not honor this volume setting, and is a known issue with Apple's simulator.

Default: 1

# Methods

# cancelAllThumbnailImageRequests

Availability
3.6.0
0.9
9.2.0
cancelAllThumbnailImageRequests() void

Cancels all pending asynchronous thumbnail requests.

Asynchronous thumbnail requests initiated with requestThumbnailImagesAtTimes.

Returns

Type
void

# pause

Availability
0.9
0.9
9.2.0
pause() void

Pauses playing the video.

Returns

Type
void

# play

Availability
0.9
0.9
9.2.0
play() void

Starts playing the video.

Returns

Type
void

# release

Availability
0.9
0.9
9.2.0
release() void

Releases the internal video resources immediately.

This is not usually necessary but can help if you no longer need to use the player after it is used to help converse memory.

Returns

Type
void

# requestThumbnailImagesAtTimes

Availability
3.6.0
0.9
9.2.0
requestThumbnailImagesAtTimes(times, option, callback) void

Asynchronously request thumbnail images for one or more points in time in the video.

The times parameter specifies an array of time values, in seconds. For each time value, the platform generates an image representing the video at that point in time.

The callback function is invoked when a thumbnail is available.

Calling this method will cancel all pending asynchronous thumbnail requests.

Parameters

Name Type Description
times Array<Number>

Array of time values, representing offsets into the video, in seconds.

option Number

Video time precision.

callback Callback<ThumbnailResponse>

Callback to invoke when a thumbnail is available.

Returns

Type
void

# stop

Availability
0.9
0.9
9.2.0
stop() void

Stops playing the video.

Returns

Type
void

# Events

# click

Availability
0.9

Fired when the device detects a click against the view.

There is a subtle difference between singletap and click events.

A singletap event is generated when the user taps the screen briefly without moving their finger. This gesture will also generate a click event.

However, a click event can also be generated when the user touches, moves their finger, and then removes it from the screen.

On Android, a click event can also be generated by a trackball click.

Properties

Name Type Description
y Number

Y coordinate of the event from the source view's coordinate system.

x Number

X coordinate of the event from the source view's coordinate system.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# singletap

Availability
0.9

Fired when the device detects a single tap against the view.

Properties

Name Type Description
y Number

Y coordinate of the event from the source view's coordinate system.

x Number

X coordinate of the event from the source view's coordinate system.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# touchcancel

Availability
0.9

Fired when a touch event is interrupted by the device.

A touchcancel can happen in circumstances such as an incoming call to allow the UI to clean up state.

Properties

Name Type Description
y Number

Y coordinate of the event from the source view's coordinate system.

x Number

X coordinate of the event from the source view's coordinate system.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# touchend

Availability
0.9

Fired when a touch event is completed.

On the Android platform, other gesture events, such as longpress or swipe, cancel touch events, so this event may not be triggered after a touchstart event.

Properties

Name Type Description
y Number

Y coordinate of the event from the source view's coordinate system.

x Number

X coordinate of the event from the source view's coordinate system.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# touchmove

Availability
0.9

Fired as soon as the device detects movement of a touch.

Event coordinates are always relative to the view in which the initial touch occurred

Properties

Name Type Description
y Number

Y coordinate of the event from the source view's coordinate system.

x Number

X coordinate of the event from the source view's coordinate system.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# complete

Availability
0.9
0.9
9.2.0

Fired when movie playback ends or a user exits playback.

On iOS, the reason property is only included if this information is available.

Properties

Name Type Description
reason Number

Reason that playback ended.

success Boolean

Indicates if the video was played successfully. User exit counts as a success. Returns true if reason is not VIDEO_FINISH_REASON_PLAYBACK_ERROR, false otherwise.

error String

Error message, if any returned. Will be undefined if success is true.

code Number

Error code. Error code will be 0 if success is true, nonzero otherwise. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# durationavailable

Availability
3.0.0
3.0.0
9.2.0

Fired when the video duration is available.

Properties

Name Type Description
duration Number

Video duration, in milliseconds.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# error

Availability
0.9
0.9
9.2.0

Fired when movie playback encounters an error.

Properties

Name Type Description
message String

Reason for error as a string.

success Boolean

Indicates a successful operation. Returns false.

error String

Error message, if any returned. May be undefined.

code Number

Error code. If the error was generated by the operating system, that system's error value is used. Otherwise, this value will be -1.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# fullscreen DEPRECATED

Availability
0.9

DEPRECATED SINCE 7.0.0

This event has been removed in Titanium SDK 7.0.0 as of the official deprecation by Apple.

Fired when a movie changes to or from fullscreen view.

On iOS, this event is fired at the beginning of the transition to fullscreen mode. A duration property is provided, indicating the duration of the animated transition to or from fullscreen mode.

Properties

Name Type Description
entering Boolean

true if the player is entering fullscreen mode, false if it is leaving fullscreen mode.

duration Number

Duration of the animated transition to or from fullscreen mode, in seconds.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# load

Availability
0.9
0.9
9.2.0

Fired when the movie play loads.


# loadstate

Availability
0.9
0.9
9.2.0

Fired when the network loadState changes.

Properties

Name Type Description
loadState Number

Current value of the loadState property.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# naturalsizeavailable

Availability
3.0.0
9.2.0

Fired when the natural size of the current movie is determined.

Properties

Name Type Description
naturalSize Number

Current value of the naturalSize property.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# playbackstate

Availability
3.0.0
3.0.0
9.2.0

Fired when the playbackState changes.

Properties

Name Type Description
playbackState Number

Current value of the playbackState property.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# playing

Availability
0.9
0.9
9.2.0

Fired when the currently playing movie changes.

Properties

Name Type Description
url String

URL of the media.

source Object

Source object that fired the event.

type String

Name of the event fired.

bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.


# preload

Availability
0.9

Fired when the movie has preloaded and is ready to play.


# resize

Availability
0.9
9.2.0

Fired when the movie player is resized.