Time

Okay, so I am trying to make an alarm clock program for computer, to familiarize myself with C#.

What I need help with, is I have the dateTimePicker tool for setting the alarm, and I have a label for displaying the current time.

My coding problem, as it were, is that I cannot figure out how to make the label display the current time, or how to get my program to play the music when the time comes around. I already have the ability to open a file, and have set the file filters for WAV, WMA, MP3, MP4, and MIDI files. so, my program can open files, but of course, at this moment, it cannot use them.

Any help would be greatly appreciated.



Answer this question

Time

  • ClaudiaHelpOnVSTO

    But using WMP SDK just adds extra bulk to something I can do with some simple API's. Now, I see your point, but that's not what I want to do. Using the WMP SDK is just adding extra bulk for something that I just want to play a few sounds.

  • maverick786us

    hmmm...this is not good..I thought one of the purposes of a language was to create new ways to do things, not to limit...

    Couldn't I create a class to play other media files in some way... Or something................to get around the net 2.0 limits

    Otherwise, I guess at the alarm's time, my alarm program could open up a playlist or any other media file in WMP 10 or 11..........


  • kanjo

    you need to add the COM component into your application, right click references in your project in solution explorer, go to add reference, select the COM tab and then add the media player (ax...something something I think). you may also wish to do a forum search as I have given some links to the documentation, which I cannot remember of by hand right now

  • Buruburu

    you shouldnt really use mciSendString/importing dll's when not needed as it defeats the purpose of .NET. you are better to use WMP SDK.

  • KipK

    I'll google that...

    is it source code, or like reference...

    eh, I'm sure if it' on the net, there's explanations to it or something...I hope

  • kangalert

    hey thanks for that timer tip. works like a charm.
    however, I can't get the dateTime control to set a time--it only sets a date. In proprties, it's value shows the current date and time, but when I select another date, say, tomorrow, I can't set a time.

    thanks again.

  • Clabab

    well you could just play raw wave file using the SoundPlayer class. Or just launch the WMP Process (not using the SDK but on the main computer) and hide it in the back

  • ianpender

    alright, found it and downloaded it, and I have been trying to put the source code in....but one problem--none of it works without a reference. I typed in alot of courde about the player, and it brought up alot of errors, so then I put some using statements for two references...only realizing that I don't HAVE those references.....

    Man, I gotta hit that C# book I bought, and read the whole thing...

  • metz-

    Oh it will let you set a time if you give it the appropriate values set in the properties.

    You may need to set it to show a custom format. So.... in the "custom format" property:

    dd/MM/yyyy hh:mm:ss

    and set the "Format" property to custom

    does this work



  • lvance1611a

    to play the audio, in .NET 2.0 there is a class that will play audio but only wave files. For MP3, I guess you would have to either launch an external player to play the file or embed the WMP SDK into your application to play the file, which allows you to play any media format WMP supports.

    to play wave files in .NET 2.0, import the System.Media namespace and use the SoundPlayer class, giving it the location and filename of the wave file, then use the "Play()" method to play the file



  • garimell

    Here's a class that I am working on, it's not finished yet, but it will play wav, mp3, wma (non-DRM) and midi:

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace AudioPlay
    {
    public static class AudioPlayer
    {
    [DllImport("winmm.dll")]
    public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

    [DllImport("kernel32")]
    public static extern int GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, int cchBuffer);

    private static string toShortFilename(string longFilename)
    {
    StringBuilder shortNameBuffer = new StringBuilder(256);
    int bufferSize = shortNameBuffer.Capacity;
    int result = GetShortPathName(longFilename, shortNameBuffer, bufferSize);

    return shortNameBuffer.ToString();
    }

    public static void Play(string Filename)
    {
    // Get short filename of the provided file
    string tmpFilename = toShortFilename(Filename);

    mciSendString("Close SND", "0", 0, 0);
    mciSendString("Open " + tmpFilename + " Alias SND", "0", 0, 0);
    mciSendString("Play SND", "0", 0, 0);
    }

    public static void Stop()
    {
    mciSendString("Stop SND", "0", 0, 0);
    }
    }
    }

  • Josh Smith

    well there will be ways for you to do it but it will take time and too advanced, .NET supports "raw" stuff and after all, it is development :-)

    you can probably create your own engine using DirectX to play MP3 but best not to "reinvent" the wheel. using the WMP SDK in your own project is perfect - there's always something there you can include in your own project to do something that doesnt exist by default.



  • Mayer Thomas

    use a timer control and create an interval of say, every minute (1000 milliseconds) and enable it. This will tick every minute and on every tick, check the current time (DateTime.Now) compared with the time set (which should also be set into a DateTime variable for ease of use when comparing) Also in this event you would set the label to the current time:

    this.theLabel.Text = DateTime.Now.ToString();

    or just to set the time:

    this.theLabel.Text = DateTime.Now.ToString("HH:mm:ss");

    simple logic when checking time:

    if DateTime.Now Equals set DateTime

    { ... play file ... }

    does this help



  • Matty1989

    hmmmm, maybe I should check ALL the properties before I ask. Thanks, now how do I make a program play an audio file I think I can get the rest to work then...

    Because what I was thinking, I have a button for setting the time, and for its click event handler, I should maybe use an if statement, like if the lbTime's text equals the dateTimepickers value, then it would play the song.....

    I just need the coding

    Thanks again.

  • Time