sia
sia

Reputation: 1910

dynamically loaded .net assembly does not work in C#

I need to run .wav file in windows app by using only MediaPlayer class and need to load the assemblies require for MediaPlayer class at runtime. I need to use PresentationFramework.dll,WindowsBase.dll and PresentationCore.dll. The object creation of MediaPlayer is successfully but when trying to invoke MediaPlayer constructor.. it fails. Below is code :

Assembly assembly = Assembly.LoadFrom("PresentationCore.dll");
Type type = assembly.GetType("System.Windows.Media.MediaPlayer");
object instanceOfMyType = Activator.CreateInstance(type);

   type.InvokeMember("MediaPlayer", BindingFlags.DeclaredOnly |
            BindingFlags.Public | BindingFlags.NonPublic |
            BindingFlags.Instance | BindingFlags.SetProperty, null, instanceOfMyType,null);

type.InvokeMember("Open", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, instanceOfMyType, new object[] { new Uri("C:\\MyMusic\\Song1.wav") });
            type.InvokeMember("Play", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, instanceOfMyType, null);

But does not work .. it gives error at this line

   type.InvokeMember("MediaPlayer", BindingFlags.DeclaredOnly |
            BindingFlags.Public | BindingFlags.NonPublic |
            BindingFlags.Instance | BindingFlags.SetProperty, null, instanceOfMyType,null);

Do i need to load all the assemblies or PresentationCOre.dll is only required. Not user how i run this.

Reference used -> http://edu-kinect.com/blog/2014/07/10/reflection-examples-c/

Thanks,

Upvotes: 0

Views: 401

Answers (1)

andrei.ciprian
andrei.ciprian

Reputation: 3025

Considering this sequence works properly (you need Windows Media Player > 10 on WinXP or the media features enabled in later OSes), not mentioning it's a bad idea just as @HansPassant mentioned:

var mediaPlayer = new System.Windows.Media.MediaPlayer();
mediaPlayer.MediaFailed += (o, ev) => 
    Debug.WriteLine("Media Failed: {0}", ev.ErrorException.Message);
mediaPlayer.MediaOpened += (sender, e) => Debug.WriteLine("Media Opened");
//string mediaFilePath = 
Uri uri = new Uri(mediaFilePath, UriKind.RelativeOrAbsolute);
mediaPlayer.Open(uri);
mediaPlayer.Play();

then reflecting the above sequence like this should work too:

Type type = Type.GetType("System.Windows.Media.MediaPlayer, PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
object instanceMediaPlayer = Activator.CreateInstance(type);
// string mp3FilePath = ...;
var uri = new Uri(mp3FilePath, UriKind.RelativeOrAbsolute);
BindingFlags bfMethodCall = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public;
type.InvokeMember("Open", bfMethodCall, null, instanceMediaPlayer, new object[] { uri });
type.InvokeMember("Play", bfMethodCall, null, instanceMediaPlayer, null);

Moreover I see you are using a wav file as source. If you need to play only uncompressed wav files (no other media formats or compressed wavs) and you don't want to control volume or other playback aspects then the more lightweight SoundPlayer should suffice, without loading any extra references.

var soundPlayer = new System.Media.SoundPlayer();
soundPlayer.SoundLocation = @"pathTo.wav";
soundPlayer.Load();
soundPlayer.Play();

Upvotes: 1

Related Questions