Friday, August 26, 2016

Playing MP3 files in Dolphin and other mobile browsers

Here is a trick to overcome the limitations of modern mobile browsers which are o-so-reluctant to  to unify their standards for the benefit of consumers and programmers. It appears that those browsers do not mind playing MP3 in video controls, so here you go:

 function playVid() {
 var myems = document.getElementsByTagName("em");
 for (var i = 0; i < myems.length; i++) {
 var myem = myems[i];
 var myemid = myem.id;
 myem.onclick = function () {
 var video = document.getElementById('video1');
 sndfile = this.id + ".mp3";
 video1.src = sndfile;
 video.load();
 video.play();}
  }
  }

The function loops through EM element IDs and plays MP3 files in onclick event. Place this function in body onload event.

Place a video element somewhere in a tucked away location on your web page and set its width and height to 1px.

 <video id="video1" width="1" height="1" > <source src=""/></video>

You can see this code in action on this interactive transcript page


SRT To Text Online Converter

SRT To Text Online Converter is a useful tool for those who want to take advantage of the closed caption information provided by YouTube to create a transcript of a video. This free online converter does not require any software installations.

To create a transcript for your video, you need to download closed caption information from YouTube and then follow these easy steps

  • Copy the URL of your video in YouTube  and follow this link to create and download the raw SRT file of your video.
  • Open your downloaded SRT file in Notepad and copy its entire contents to clipboard (maximum character size: 5000)
  • Follow this link to create transcript. 
  • Once you have finished, you can fine tune your transcript file. Transcript stripped of stopwords can be useful to make a list of keywords to optimize your video for search engines or in the "Description" field on Youtube Video Manager page or video webpage on your website.
  • You can also create a list of most frequent words that occur in your transcript  for search engines optimization.
And this is free online tool to extract keywords from a webpage:

Regular expressions to match spaces, whole words and words with flexions

Regular expressions can be very useful but also very tricky. Here are a  few ones that have been tested in my VB.NET web applications.

Regular expression to match whole words surrounded by spaces


"\b\.?\s+" & yourword & "\b\s+"

\b\  stands for word boundary

\s+ stands for one or more spaces

Here is an improved regular expression without word boundaries that also looks for optional punctuation marks:

"([\.!?]?\s+" & yourword & "\s+)"

The optional group is enclosed in brackets, without them the group becomes mandatory.

Regular expression to match words with flexions

"((?i)\s+" & yourword  & "[en|e|er|s|es\b]{0,}[\.\,!?]?\s+)"

?i sets ignore case

en|e|er|s|es\b sets word flexions before word boundary

{0,} gets all forms

[\.\,!?] optional punctuation after word boundary

\s+ mandatory space

Please let me know how it works in your applications.









Extract specific number of words from string

The following function is useful where there is a need to display a snippet of text. Tested in real life  VB.NET application.

  Function GetNewString(ByVal mystr As String) As String
        Dim finStr As String = ""
        Dim count As Integer
        Dim words1() As String
        Dim strSeparator As Char = (" ")
        words1 = mystr.Split(strSeparator)
        If words1.Length - 1 < 10 Then
        For count = 0 To words1.Length - 1
        finStr &= words1(count) & " "
        Next
        Else
        For count = 0 To 10
        finStr &= words1(count) & " "
         Next
        End If
        Return finStr
    End Function

The function returns 10 words from a string that is passed as a string parameter.

Add onkeypress event to server control

Suppose you need to use a client side function when the enter key is pressed in a  a server control. This is how it can be done with a few lines of javascript code:

 function handle(e) {
     if (e.keyCode === 13) {
     var linkUrl = document.getElementById("txtLinkUrl").value;
     var linktext = document.getElementById("txtLinkText").value;
     var mylink = "<a href='" + linkUrl + "'>" + linktext + "</a>";
     var txtWriteValue = document.getElementById("txtWrite").value;
     document.getElementById("txtWrite").value = txtWriteValue + " " + mylink;
     document.getElementById("lblActive").innerText = txtWriteValue + " " + mylink;
     }
     return false;
     }

The function converts values from two textboxes into a hyperlink. The event handler is added as an attribute to a server control where the enter key is pressed:

txtWrite.Attributes.Add("onkeypress", "handle(event)")

This function does not perform any error checking, so you need to customize it to be more robust.