11

Why cant I add "windows media player" reference?

 2 years ago
source link: https://www.codeproject.com/Questions/5320534/Why-cant-I-add-windows-media-player-reference
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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:
Copy Code
using WMPLib;

public static WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
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
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
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:
Copy Code
/// <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:
Copy Code
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

:)
Comments
Thx, this is only for .wav files, what about for .mp3 files?
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
now see added2 (the circle is round)

Add your solution here

Preview

Existing Members

Sign in to your account

...or Join us

Download, Vote, Comment, Publish.

Your Email   Password  

 

Your Email   Optional Password  

StrengthToo short

 

I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the CodeProject newsletters
When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK