Javascript API

From Justin.tv API Wiki

Jump to: navigation, search

The Javascript API makes embedding Justin.tv content on your website easier. Here's how you use it:

  • Put the following tag in the head of your page:
<script src="http://www-cdn.justin.tv/javascripts/jtv_api.js"></script>


The Static Approach

<div name="{{method}}" title="{{options}}"></div>

Example:

<div name="jtv_api.new_player" title="channel=officecam&start_volume=50"></div>

The Dynamic Approach

Methods take two arguments:

  • Target, which is the id or dom node of the element that the embed will be placed in
  • Options, which is an object containing the options you want to be applied to the embed


Methods return the dom node that represents the embed, which, in the case of the player, you can preform actions on (like play_live and pause_video).

Methods

Name: jtv_api.new_player

Options:

  • channel: This required option specifies what channel should be played.
  • auto_play: A boolean which specifies whether the embed should play immediately after loading. Requires a consumer_key.
  • consumer_key: This is a key which represents your application, which you can get by signing up for a developer account. It is required for auto_play and custom.
  • start_volume: How loud the sound should be on a scale from 0 to 100 when the video starts playing (default is 26).
  • watermark_position: Which corner the Justin.tv logo should appear in. Options are top_right, bottom_right, top_left, and bottom_left.
  • custom: A boolean which, when set to true, will disable all buttons and enable events.
  • events: An object mapping keys which represent event types to functions which handle those events. Valid events are:
    • buffering: Every second that the video stream has an empty buffer, this event is dispatched.
    • stop_buffering: If a stream that previously dispatched a buffering event receives data, it dispatches this event.
    • buffer_full: When the stream's buffer becomes full, this event is dispatched. The first time this event is dispatched is when live video starts playing.
    • buffer_empty: When the stream's buffer becomes empty, this event is dispatched.
    • stream_lost: When the player gives up on the stream (after waiting 10 seconds for data with no success), this event is dispatched.
    • video_not_found: If you request to play a channel live and that channel is not live, this event is dispatched.
    • connected: Whenever you successfully connect to the server to play live video, this event is dispatched.
    • started: Whenever you successfully issue a PLAY message to the server to play live video and it is acknowledged, this event is dispatched.
    • broadcast_finished: When the broadcaster ends their broadcast of a live stream you are watching, this event is dispatched.
    • total_viewer_count({'total':...}): Periodically the server will issue an update with the total number of viewers currently on Justin.tv. This event contains that value.
    • stream_viewer_count({'stream':...}): Periodically the server will issue an update with the total number of viewers currently watching your stream. This event contains that value.

Methods of returned object (if custom == true):

  • play_live(channel): Begins playing the specified channel.
  • play_playlist(channel): Play the playlist on a given channel.
  • get_volume(): Returns the current volume on a scale from 0 to 100.
  • change_volume(x): Sets the volume of the player on a scale from 0 to 100.
  • resume_video(): Resume the currently paused video.
  • pause_video(): Pause the currently playing video.
  • set_quality(type): Set the quality to the type passed. This is a string of one of "240p", "360p", "480p", "720p", or "live" (for the original, non-transcoded stream). Defaults to "360p" if availabe, "live" otherwise.
  • set_buffer_time(length): Sets the buffer time for the stream in seconds (default 2).
  • resize_player(width, height): Resizes the player to a given width and height.
  • post_screencap(to_url): Grab a screencap from the current live video, encode it as a jpeg image, and POST it to a given url.
  • set_channel_password(password): Set the password that the api swf will use for the stream, if the stream has a channel password. Must be called before play_live(channel).

Name: jtv_api.new_broadcaster

Options:

  • stream_key: A required option which specifies the stream key of the stream to broadcast to.
  • consumer_key: This is a key which represents your application, which you can get by signing up for a developer account, but don't actually need.

Name: jtv_api.new_chat

Options:

  • channel: A required option which specifies what channel the chat should be on.
  • hide_chat: Comma seperated list of chat types that should be hidden.
  • defult_chat: Chat type that is enabled once the page is loaded.
  • tweet_suffix: The string that will be appended to the Tweets of users who use the Twitter chat. Useful for appending URLs.
  • over18: Set to 'true' if user has already confirmed they are 18 on your site.

Chat types:

  • jtv
Client Libraries