Why cant I add "windows media player" reference?
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.
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();
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.dllThe 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
:)
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
Add your solution here
Preview
Existing Members
...or Join us
Your Email Password
Your Email Optional Password
Please subscribe me to the CodeProject newsletters
- Read the question carefully.
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
- 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.
- 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.
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK