Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
From COM.

When I press on the toolbox and choose: "Choose items" and then the tab COM I don't see "Windows Media Player" at all (supposed to be with a checkbox).

When I go to Reference=>Add reference, and choose COM I see Windows Media Player with version 1.0 and when I check its checkbox and press OK to add it it says:

A reference to "Windows Media Player" could not be added.

I'm on Windows 10 64 bit 21H1.

Is there a newer way to play MP3 files from resources and/or from a path on the hard disk?

What I have tried:

using WMPLib;

public static WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
Posted
Updated 21-Dec-21 5:24am
v4

I installed Windows Media Player from Apps & Features from Optional Features and now the old reference works and the code enters debugging without the error: "WMPLib not found" as it's found in the old reference.
   
Comments
CHill60 21-Dec-21 9:02am
   
Don't repost the same solution twice. Be patient - your posts both went into the moderation queue for a human to assess. We do this in our spare time and can't always respond immediately
FierceLion 21-Dec-21 9:39am
   
Ok, I removed the first, thank you.
There is a System.Media.SoundPlayer class that plays sound files or streams.

It is part of System.dll under .NET FrameWork (no extra reference needed); it has been moved to System.Windows.Extensions.dll under .NET 6 (which needs you to add a reference).

ADDED:

you can also use P/Invoke to call on the native method sndPlaySound inside winmm.dll

The code below should give you a starting point, I use it for playing sounds from embedded resources:

/// <summary>
/// Synchronously play a sound resource from the assembly that also includes type.
/// </summary>
/// <param name="type"></param>
/// <param name="resourceName"></param>
public static void PlaySound_old(Type type, string resourceName) {
    try {
        // inspired by http://www.bobpowell.net/playsnd.htm
        if (type == null) type = typeof(LP_Resource);
        resourceName = type.Namespace + ".resources." + resourceName;
        Stream stream = type.Assembly.GetManifestResourceStream(resourceName);
        int len = (int)stream.Length;
        byte[] buf = new byte[len];
        stream.Read(buf, 0, len);
        GCHandle handle = GCHandle.Alloc(buf);
        IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buf, 0);
        int res = sndPlaySound(ptr, 4);   // SND_MEMORY
        handle.Free();
        log("Played sound " + resourceName);
    } catch (Exception e) {
        log(resourceName + " failed: " + e.Message);
    }
}

[DllImport("Winmm.dll", CharSet = CharSet.Unicode,
     CallingConvention = CallingConvention.StdCall)]
internal static extern int sndPlaySound(IntPtr buffer, int dwFlags);

Alas, that too only handles .wav sounds.


ADDED2:

the only way I've found so far to play MP3 from C# is using WMPLib:

WMPLib.WindowsMediaPlayer player = new WMPLib.WindowsMediaPlayer();
player.URL = absoluteFilePath;
player.controls.play();

which needs an absolute file path (like C:\...\filename.mp3) or an actual URL.
I see no way to play a sound resource, without temporarily creating a file that is.

In order to use WMPLib, one has to add a reference:
- references
- add reference
- COM
- search "Windows Media Player", the tooltip should mention C:\WINDOWS\system32\wmp.dll

:)
   
v7
Comments
FierceLion 21-Dec-21 14:27pm
   
Thx, this is only for .wav files, what about for .mp3 files?
Luc Pattyn 21-Dec-21 14:59pm
   
You're right.

I've added an alternative to my original post. It is however obsolete, and also limited to .wav files.

Working on something new...

BTW: a decent mp3-to-wav converter can be found here
Luc Pattyn 21-Dec-21 15:46pm
   
now see added2 (the circle is round)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900