Click here to Skip to main content
Click here to Skip to main content
Go to top

Add multimedia effect to system clock

, 3 Sep 2004
Rate this:
Please Sign up or sign in to vote.
This article focuses on how to add a virtual multimedia effect to the system clock.

Sample Image - clock.jpg

Introduction

In this program, I have tried to add the dose of multimedia to the system clock. At every new hour, it plays an .mp3 file that ticks the number of times the current hour is.

For this, the program has to be loaded on Windows startup. So with this program, you can also automatically start programs whenever Windows launches. We refer to it as loading applications on Windows startup. If you have programs automatically starting that you have not loaded, then you can remove them using this code as well.

How’s it possible?

Loading an application on Windows startup is easy. You could manually do it using regedit.exe – a registry editor.

Open your registry and find the key: [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]. For each program you want to start automatically, create a new string value using a descriptive name, and set the value of the string to the program executable. For example, to automatically start Notepad, add a new entry of "Notepad"="c:\windows\notepad.exe".

Now, we will implement the same thing in our program. Instead of Notepad, we will load our own program “AudioClock.exe” on Windows startup.

VB.NET and Registry

Editing registry via VB.net requires importing Microsoft.Win32 namespace that provides us the Registry and RegistryKeyClass.

Non-inheritable Public Class Registry provides the set of standard root keys found in the registry on machines running Windows. The keys are all read-only since the registry depends on their existence.

RegistryKey class is a registry encapsulation and represents a key level node in the Windows registry. RegistryKeys are the base units of organization in the registry, and can be compared to folders in Windows Explorer. A particular key can have subkeys (just as a folder can have subfolders), and can be deleted, as long as the user has the appropriate permissions to do so and the key is not a base key, or the level directly under the base keys. Each key can also have multiple values associated with it (a value can be compared to a file), which are used to store the information about the application you are interested in. Each value holds one particular piece of information, which can be retrieved or updated when required.

Code analysis

It’s time we had some coding.

Importing necessary namespaces

'for accessing windows registry
Imports Microsoft.Win32
'for using Timer 
Imports System.Windows.Forms 
'for getting current directory
Imports system.IO

Declarations

Private thisTime As Date 
Private thisHour As Integer 
Private directory As directory
Dim filename As String

Creating new string value

Private Sub writeToRegistry () 
    Dim regKey As RegistryKey
    regKey = _
      Registry.CurrentUser.OpenSubKey(" SOFTWARE\" & _ 
          "MICROSOFT\WINDOWS\CURRENTVERSION\RUN", True)
    fileName = directory.GetCurrentDirectory().ToString + "\audioClock.exe"
    regKey.SetValue ("audioClock", fileName)
    regKey.Close ()
End Sub

The above code snippet instantiates the RegistryKey using the static member OpenSubKey. The Boolean parameter “TRUE” provides a write access to the particular key. A new string value “audioClock” is added with value equal to the full path and filename of our current application. Remind you, our application name is “audioClock.exe”. Change in its name requires the change to be made here. Please note the backslash before audioClock.exe.

When to tick?

Now, the time has come to add a dose of multimedia to Windows system clock. We want a .mp3 file to be played at every new hour, so what we need to keep track of is the system clock time. Use this simple arithmetic implementation to get the first tick.

Private Sub startClocking ()
    thisTime = TimeOfDay
    'check to see how many minutes and seconds 
    'is required to reach the next hour ?
    newTimer.Interval = (60 - thisTime.Minute) _
            * 60 * 1000 - thisTime.Second * 1000
    newTimer.Start ()
End Sub

It should be noted that the timer interval is expressed in milliseconds.

Setting new Timer interval after the first tick has occurred

Once the clock has ticked first time, the next tick will occur after one hour. So, the new interval is set to one hour.

Private Sub continueClocking ()
    'the new interval is one hour
    newTimer.Interval = 60 * 60 * 1000
    newTimer.Start ()
End Sub

Time to play a .mp3 file

'handles the tick event of the newTimer
Private Sub newTimerTickEvent ( ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles newTimer.Tick
    command= "mplay32.exe -play" + " "" " + getFilenameToPlay(thisTime) + " "" "
    Shell( command, AppWinStyle.Hide)
    continueClocking ()
End Sub

The .mp3 file is played using the Media Player application that comes with the Windows OS. The application name is mplay32.exe. As you can see, I have used a shell function to launch the mplay32.exe program without displaying it. After all, we don’t want the Media Player to pop up whenever the music plays, do we?

The function Private Function getFilenameToPlay ( ByVal thisTime AsDate) As String returns the full path and filename of a file to be played.

What more can you do with this code?

This code has enough room for improvement and enhancement. It can be used in a wide range of applications viz., alert messaging applications, stop watch, reminders as that of Microsoft Outlook, automatic execution of other applications (automatic dialup connections!), shut down/restart/hibernate automatically at some X time, and many more. Truly speaking, it can be implemented in any kind of application that requires keeping track of system time.

Limitation

It keeps track of system time only once on Windows startup. Thereafter, it uses its own timer to tick at every new hour. Any change in the system clock after the Windows startup is not detected until next startup. The limitation is a tradeoff against simplicity.

While writing this code …

Playing with registry has always become a fun part of programming. It provides a sense of power and superiority. Playing with system time added extra doses of control and authority.

Handle with care …

Modifying the registry can cause serious problems that may require you to reinstall your operating system. Administrative/Proper privilege is required for the modification.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

A.k.joshi
Engineer
Hong Kong Hong Kong
.

Comments and Discussions

 
Questionall users PinmemberRobinhere24-Jun-07 5:19 
GeneralneHuge BBCRadio Software PinsussNandkishor Arvind Dheka16-Aug-05 0:16 
Generalsecurity_system Pinmemberthanchetrolai23-Mar-05 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 4 Sep 2004
Article Copyright 2004 by A.k.joshi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid