How to create a Karaoke machine in Silverlight

(Scroll to the bottom for the nice UI)

Figure: Karaoke machine UI done by a Developer :-)


  1. We have 2 textboxes for artist and title
  2. On click of “Start fun” button we call a web service to get the lyrics of the requested song
  3. After that we scroll the lyrics in the textbox below

See how easy that is, with Silverlight!!!

System overview

Figure: Silverlight application calls the local web server, which calls the Lyrics web server

It’s that complicated, because cross domain access is blocked in Silverlight and the Lyrics web service doesn’t have a clientaccesspolicy.xml file



“Silverlight SDK Toolkit”

Download “Silverlight SDK Toolkit” from MSDN

Create a new Silverlight Application project

Figure: Create a Silverlight project

Figure: Yes, Add a ASP.NET web project

Create a reference to the external Lyrics web service

  1. Right click on “SilverlightApplication.Web”
  2. Click on “Add Web Reference”
  3. Use this URL to reference the web service
  4. Click on Add Reference to finish

Create the web service proxy that calls the external Lyrics web service

  1. Right click on “SilverlightApplication.Web”
  2. Add New item
  3. Choose “Web Service”
  4. Name it “LyricService”
  5. Enter a new method called GetSong
public org.lyricwiki.LyricsResult GetSong(string artist, string title)
    org.lyricwiki.LyricWiki lyricWiki = new org.lyricwiki.LyricWiki();
    return lyricWiki.getSong(artist, title);

Build your solution!

Create a reference to the web application proxy service

Create the layout (page.xaml)

<UserControl x:Class="SilverlightApplication.Page"
    Width="400" Height="300">
        <TextBlock  Text="Artist"></TextBlock>
        <TextBox x:Name="textBoxArtist" Width="200" Margin="5" Height="50"></TextBox>
        <TextBlock  x:Name="Artist" Text="Title"></TextBlock>
        <TextBox x:Name="textBoxTitle" Width="200" Margin="5" Height="50"></TextBox>
        <Button x:Name="buttonStart" Content="Start fun!" Margin="10" Width="100" Height="50" Click="buttonStart_Click"></Button>
        <TextBlock x:Name="textBlockResult" FontSize="20"  Text="nothing so far" Margin="10" Height="30" Width="700"></TextBlock>
Figure: XAML for the ugly Karaoke player

Code! Finally :-)

Use a timer to tick every 100 milliseconds and scroll through the lyrics

DispatcherTimer timer = new DispatcherTimer();  
timer.Interval = new TimeSpan(0, 0, 0, 0, 100);  
timer.Tick += new EventHandler(timer_Tick);

On each timer tick, we scroll the text

private void ScrollText()
// Scroll 1 caret to the left, I know thats not nice :-) but easy!
textBlockResult.Text = textBlockResult.Text.Substring(1) + textBlockResult.Text.Substring(0, 1);

Create the event handler on song completed call

    //No code, tooo easy

Create the timer and start it after song completed call

    //No code, tooo easy



Remember: Everything is async in Silverlight

Advanced Tasks

1. Add a slider for the speed of the text


Figure: Slider for adjusting the speed of the text scrolling

2. Make your Silverlight application look pretty


3. Make the text show only 1 line full, the other lines with low opacity

Google for: Silverlight Opacity

4. Scroll the text line by line (instead by characters)

Search for the first Newline in the text and split the text there

5. Make the application go in full screen after start

Have a look at

6. If no title entered, let the user choose from a list of songs by the artist


7. Make the song scroll like Star wars



8. Embed a youtube video next to the text of that song

See for details on calling the youtube webservice

No comments:

Post a Comment

Latest Posts

Popular Posts