AdSense

Friday 23 August 2013

A music player with C#

(Deutsche Version) There are a lot of music players on this world. Every player has its advantages and disadvantages. Until now, I was not able to find the "perfect" music player. Therefore I wrote my own music player which can be downloaded here. A bit about the handling: At first, a folder has to be specified as music library. Then, by clicking on "Refresh Library", all music files in this directory are laoded. Hotkeys were very important for me, so I implemented them (see below). I will present two important parts of the music player: Playing music with WPF and using Hotkeys. By the way, you can find a basic introduction to WPF here: C# Tips and Tricks.

Playing music

Hence I use WPF, playing music is very simple: In my xaml file, I have a MediaElement:

<MediaElement Height="10" Width ="10"  LoadedBehavior="Manual" Name="mediaElement1" VerticalAlignment="Top" />

The MediaElement is invisible. To play music, you have to execute the following code:

mediaElement1.Source = new Uri(pathToMediaFile);
mediaElement1.Play();


This is the whole magic for playing music. You can now do several things like registering to events so you know when the actual song has finished playing (to start the next song).

Hotkeys

Note: By now I use a "better" implementation of the hotkeys which you can find here.

Using hotkeys is pretty simple. At first, you have to add several lines into the program:

[DllImport("User32.dll")]
private static extern bool RegisterHotKey(
    [In] IntPtr hWnd,
    [In] int id,
    [In] uint fsModifiers,
    [In] uint vk);

[DllImport("User32.dll")]
private static extern bool UnregisterHotKey(
    [In] IntPtr hWnd,
    [In] int id);

private HwndSource _source;
private const int HOTKEY_ID = 9000;

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    var helper = new WindowInteropHelper(this);
    _source = HwndSource.FromHwnd(helper.Handle);
    _source.AddHook(HwndHook);
    RegisterHotKey();
}

protected override void OnClosed(EventArgs e)
{
    _source.RemoveHook(HwndHook);
    _source = null;
    UnregisterHotKey();
    base.OnClosed(e);
}

private void UnregisterHotKey()
{
    var helper = new WindowInteropHelper(this);
    UnregisterHotKey(helper.Handle, HOTKEY_ID);
}

private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    const int WM_HOTKEY = 0x0312;
    switch (msg)
    {
        case WM_HOTKEY:
            OnHotKeyPressed(wParam.ToInt32());
            break;
    }
    return IntPtr.Zero;
}


As a next step, I defined the three hotkeys I need:

private void RegisterHotKey()
{
    var helper = new WindowInteropHelper(this);
    const uint VK_RIGHT = 0x27;
    const uint VK_LEFT = 0x25;
    const uint VK_SPACE = 0x20;
    const uint MOD_ALT = 0x0001;
    const uint MOD_CTRL = 0x0002;

    if (!RegisterHotKey(helper.Handle, HOTKEY_ID+2, MOD_CTRL + MOD_ALT, VK_RIGHT))
    {
        // handle error
    }
    if (!RegisterHotKey(helper.Handle, HOTKEY_ID+1, MOD_CTRL + MOD_ALT, VK_LEFT))
    {
        // handle error
    }
    if (!RegisterHotKey(helper.Handle, HOTKEY_ID, MOD_CTRL + MOD_ALT, VK_SPACE))
    {
        // handle error
    }
}


Finally, I need a function which executes the desired actions when a hotkey is pressed:

private void OnHotKeyPressed(int key)//9000 = space, 9001 = left, 9002 = right
{
    //System.Console.Write("KeyPressed! {0}\n", key);
    switch (key)
    {
        case 9000: if (playing) { pause(); } else { play(); }
            break;
        case 9001: back();
            break;
        case 9002: next();
            break;
        default:
            break;
    }
    // do stuff
}


Please note: If you have a hotkey registered, e.g. "AltGr" + "0", this key combination is only available for my program. So in this example, I could not write } anymore (I have a german keyboard layout) as long as my program is opened.

No comments:

Post a Comment