One of the great aspects of Viddler’s API is the ability to embed our webcam recorder right into your site. With only a few lines of code, your users will be able to record video using any camera attached to their computer, and it’s automatically uploaded to Viddler and encoded. Let’s dive right in!
First off, if you haven’t checked out part one and part two of the phpViddler series, you’ll want to do that first, as some of that code will be necessary for recording.
As explained on the wiki page, there are 2 steps to embedding the video recorder on your site:
- Get a record_token using either Viddler.users.auth with get_record_token set to “1″ or Viddler.videos.getRecordToken.
- Use the returned record_token to embed the recorder on your site.
Getting A Record Token
In order to embed the video recorder, you’ll need to pass a “record token,” which is a one-time use key that allows the user to record a video. There are two ways to get the record token, so I’ll go through both.
During User Authentication
When you use the user_authenticate()
function (covered in part 2), you can pass a third parameter to return a record token, which is then returned along with the sessionid:
<?php $sessionResponse = $viddler->user_authenticate('user', 'password', '1'); $recordToken = $sessionResponse['auth']['record_token']; ?>
After Authentication
If you already have a valid sessionID, but you haven’t yet generated a record token, you can use the video_getrecordtoken()
method, which takes a sessionID as a parameter. Here’s how you’d use it:
<?php $recordToken = $viddler->video_getrecordtoken($sessionID); ?>
Personally, I prefer grabbing the token during authentication, since it cuts down on the number of API calls I have to make, but it really depends on the situation.
Note: While sessionIDs expire, as you no doubt learned in part two, record tokens never expire but can only be used once. So if you generate a record token for the user when they login, you can keep this token until it is used for as long as you’d like.
Embedding the Video Recorder
Now that you have your record token, it’s time to embed the video recorder. Thankfully, phpViddler makes it easy again with the video_getRecordEmbed()
function. To use it, just pass it a record token:
<?php echo $viddler->video_getRecordEmbed($recordToken); ?>
And with that, your users are ready to record videos right from your site!
Knowing When Recording is Finished
After your user successfully records a video, you’ll likely want to send them to a new page, or call some other action. Luckily, the video recorder calls a javascript function, recordDone()
when it’s finished. To implement this, you’ll have to add something like the following to your record page:
<script language="javascript"> function recordDone(username, user_video_number, video_id) { // Alert the user that recording is finished alert('Thanks '; + username + ', you've recorded video #' + user_video_number + ' with id ' + video_id); // Send user to new page top.location('http://newpage.com'); } </script>
As you can see, recordDone()
receives 3 arguments, which you can use however you’d like:
- username: The user who recorded a video
- user_video_number: The number that appears in the video’s Viddler URL, e.g. http://www.viddler.com/explore/kyleslat/videos/24/
- video_id: The video’s unique ID, which can be used to embed the player.
So, there you have it, with a few simple lines of code, users are now able to record video directly from your site! Next up, we’ll be tackling uploading videos, so stay tuned.