|
Write a comment |
Articles |
DHTML Gallery |
.NET |
MyBlog |
About Me |
FAQ
|
|
|
Adding Subtitles to videos in Silverlight
Creating the XAML <Canvas Width="320" Height="240" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Background="Black"> <MediaElement x:Name="VideoControl" Canvas.Top="0" Canvas.Left="0" Source="http://localhost/SlvrSub/test.wmv" AutoPlay="true"> </MediaElement> <Canvas Background="#40000000" Width="320" Height="20" Canvas.Top="220" Canvas.Left="0" Opacity="255"> <TextBlock FontSize="12" x:Name="Subtitle" FontFamily="Arial Unicode MS" Canvas.Left="40" Foreground="White"> Javascript needs to be turned on to show subtitles.</TextBlock> </Canvas> </Canvas>
Writing Javascript
var sub_en = new Array();
sub_en[1] = "Subtitle as 1st Second";
sub_en[2] = "Subtitle as 2nd Second";
sub_en[3] = "Subtitle as 3rd Second";
sub_en[4] = "Subtitle as 4th Second";
sub_en[5] = "Subtitle as 5th Second";
// ..... Add till last second of video.
var slvr_ctrl,videoCtrl,subobj;
var persistTextSec = 5;
var oldTitlePos = 0;
//Initialize variables. Called on 'OnInit'
// of Silverlight Plugin.
function Init(sender)
{
subobj = new SubTitler();
subobj.SubtitleDataSource = sub_en;
slvr_ctrl = sender.getHost();
videoCtrl = slvr_ctrl.content.findName("VideoControl")
//Set a javascriptimer
window.setTimeout("timerCompleted()",100);
}
//If the video is playing get subtitles corresponding to current
// position of the video.
function timerCompleted()
{
if(videoCtrl.CurrentState == 'Playing' || videoCtrl.CurrentState == 'Opening')
{
var sec = Math.round(videoCtrl.Position.seconds);
var lbl = slvr_ctrl.content.findName("Subtitle");
var subtxt = subobj.GetSubtitle(sec);
if(subtxt)
{lbl.Text = subtxt; oldTitlePos = sec;}
else
{ if(sec - oldTitlePos > persistTextSec) lbl.Text = "";}
}
window.setTimeout("timerCompleted()",100);
}
//Error handler
function Err(a,z)
{alert(z.errorCode+" "+z.errorMessage);}
The 'Init' function get called when the plugin loads. It initializes global variables and sets off a javascript timer. Though the timer is triggered every 100 millisecond, subtitles can only be changed per second. When the timer elapses 'timerCompleted' is called. It simply pulls out the subtitle corresponding to current position (in seconds) of the video and draws it on top of the video. Since we care of showing subtitles with only second accurracy, we round off the current position of video and get the corresponding subtitle text (since it turns out to be a floating point number). The 'persistTextSec' value is number of seconds a subtitle should continue to show on the screen, if no subtitles are found for consequtive seconds. If you want to simply test subtitling for long video, then you can use a javascript loop to initialize subtitle array to dummy strings
//Intializes subtitles with dummy strings
//for first hundred seconds of playback.
for(i=0;i<100;i++)
{sub_en[i]= "Subtitle at" + i;}
Creating HTML file <html> <head><title>Subtitles Demo</title> <script type="text/javascript" src="Subtitler.js"></script> <script type="text/javascript" src="Subtitles.js"></script> </head> <body> <object type="application/x-silverlight" height=240 width=320> <param NAME="Source" VALUE="VideoPlayer.xaml"> <param NAME="MaxFramerate" VALUE="30"> <param NAME="Windowless" VALUE="0"> <param NAME="OnError" VALUE="Err"> <param NAME="OnLoad" VALUE="Init"> </object> </body></html> That was easy wasn't it. If you use this technique in your please share it with me so that I can put a link here on my website. As always appreciation or criticsm are always welcomed. |
| Copyright (c) 2007-2008 Ashish Patil . Please read FAQ for more details. |