Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
<br />
<br />
QUESTION HAS BEEN SOLVED. I WILL POST THE SOLUTION SHORTLY. <br />



After adding windows media player library (wmp.dll), I'm able to play mp3's in my c# application.

var player = new WMPLib.WindowsMediaPlayer();


Current syntax:

player.URL = @"K:\Sounds\8.mp3";


I've added the mp3's and images to the resources in the references.
Now images can be called this way:

pictureBox[0].Image = WindowsFormsApplication1.Properties.Resources.cleff;


But that method doesn't seem to find the mp3's in the resources.

<code>player.URL = WindowsFormsApplication1.Properties.Resources. - mp3's missing -;
//Therefore this way it doesn't work.
</code> 


Anyone knows how to call the mp3 files in the resources to be played? thank you.
Posted
Updated 20-Mar-11 8:46am
v2
Comments
Albin Abel 20-Mar-11 10:39am    
If you ask me it is better to keep the mp3 external. Why unnecessary load on the memory? The song is not going to sing continuously as user may want to stop it. You are taking away the flexibility of the song choice in terms of maintainability
Sergey Alexandrovich Kryukov 20-Mar-11 15:17pm    
Agree with you. I put forward 3 options in my Answer, please see. Your way is the simplest one, only OP needs to learn some cultural use of paths.
--SA
LighthouseCall 20-Mar-11 10:44am    
I'm building a simple piano. Therefore the sound file is like 2 seconds in length. The problem is portability, I work on it both at home, school, etc. If i leave the player.URL at K: drive, if the program is launched from another port, for example E: , the sounds won't load. Hence I need them in the resources. I don't know if I'm explaining myself enough.. Sorry if I'm not;
Sergey Alexandrovich Kryukov 20-Mar-11 13:51pm    
This is merely based on incorrect work with file path names. You need to calculate path name every time you get the file relative to something, such as your assembly main module's executable file.
--SA
LighthouseCall 20-Mar-11 14:42pm    
Hence why I needed them in the resources. Anyways it's fixed now.

Approach #1. If you really need a way to stream a video from the resource, you need to create a resource stream using the resource you need. This will give you an idea: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/98f149e9-6975-4d67-a2c7-7e5c93c8cfc2[^], http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].

Feeding the stream read from resource as the input of your Media Player control is difficult. The only input is URI. I could not find that a special Microsoft URL schema "res://" supported. As a general approach, you need to develop a special URI instead. You will need to use the constructor for the System.Uri: Uri(SerializationInfo, StreamingContext). For more information, see http://msdn.microsoft.com/en-us/library/system.uri.aspx[^].

This approach will take some effort.

Approach #2. At the same time, I agree with Albin. You will be much better off using external files. You problem is using wrong path names. Your media files are read-only (otherwise you would not put them in your resources). You should put them with your assembly executable files. Find out where your entry assembly has a main module executable and put your media files there or in some relative position. This is how:

C#
string exePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);


Approach #3 — intermediate. Put media in resources, on request, read the resource as a binary stream and write into temporary file; pass the file as URL to the Media Player. Again, here you need some culture of working with paths. Use System.IO.Path.GetTempFileName.

I would recommend Approach #2, in agreement with Albin.

—SA
 
Share this answer
 
Comments
Albin Abel 21-Mar-11 0:53am    
Now understood why OP needs it. Your choices are great. my 5+
Sergey Alexandrovich Kryukov 21-Mar-11 0:57am    
Thank you very much.
--SA
Just as a help 1) I would agree keep the files seperate not in resources and 2) If your looking for a good way to play MP3 and other files using WMP please see my article: Media Player Class[^]
 
Share this answer
 
Comments
Albin Abel 21-Mar-11 0:55am    
Thanks for your agreement and I prefer the choices by SAKryukov as well. It is a good article. My 5+

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