You need to write an EXE file, which is embedded with a piece of music that I designed. After opening the EXE file, the music will be automatically played. The most important thing is that there cannot be any additional files except the EXE file. Copy the EXE file to another computer (with framework installed) and run it properly.
1. Resource embedding
This is relatively simple. vs2008 provides a very convenient method. After adding a project, open resources in the properties folder. resx file, click "Add Resource" to add an existing file, and select the MP3 file you want to add. vs2008 will automatically add an MP3 file name to the resources class, for example, my dream is true ", the automatically generated attributes are as follows:
Internal static byte [] dream come true {
Get {
Object OBJ = ResourceManager. GetObject ("dream come true", resourceculture );
Return (byte []) (OBJ ));
}
}
This attribute can be accessed using properties. resources. It is very convenient.
2. play MP3
The Media Player control is usually used for playback. However, if the media player control is added, a wmp. dll file is added, which does not meet the requirements. Therefore, an API function is used for playback.
[Dllimport ("winmm. dll")]
Static extern int32 mcisendstring (string command,
Stringbuilder buffer int32, buffersize, intptr hwndcallback );
The specific usage of this function can be viewed in msdn, which is very detailed. Here we only need to encapsulate three commands, open \ play \ close.
/// Open a media file
Void openmedia (string path)
{
Mcisendstring ("open" + path + "alias Media", null, 0, handle); // use the open command to open the file
Mcisendstring ("play media", null, 0, handle); // use the player command to play the video.
}
/// Close all open media files
Void closemedia ()
{
Mcisendstring ("close all", null, 0, handle); // use the close command to close
}
3. Release embedded MP3 resources for playing
Because the embedded resource is accessed in the form of byte [], all resources must be saved as an object file before playing.
Private string getresmp3 ()
{
String Path = path. Combine (application. startuppath, "Dream success ");
Filestream FS = new filestream (path, filemode. Create );
FS. Write (properties. Resources. dream come true, 0, properties. Resources. Dream come true. Length); // write all the content in byte [] to the file.
FS. Close ();
Return path;
}
With the above three steps, it is easy to play the embedded MP3 file. The following is the completeCode
Public partial class form1: Form
{
[Dllimport ("winmm. dll")]
Static extern int32 mcisendstring (string command,
Stringbuilder buffer int32, buffersize, intptr hwndcallback );
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
String Path = getresmp3 ();
Openmedia (PATH );
}
Private string getresmp3 ()
{
String Path = path. Combine (application. startuppath, "Dream success ");
Filestream FS = new filestream (path, filemode. Create );
FS. Write (properties. Resources. dream come true, 0, properties. Resources. Dream come true. Length );
FS. Close ();
Return path;
}
Void openmedia (string path)
{
Mcisendstring ("open" + path + "alias Media", null, 0, handle );
Mcisendstring ("play media", null, 0, handle );
}
Void closemedia ()
{
Mcisendstring ("close all", null, 0, handle );
}
Private void form=formclosed (Object sender, formclosedeventargs E)
{
Closemedia ();
}
}
Attachment: it encapsulates the executable file of the song "dream come true". I hope you will like this song.
Executable File Download