C # MP3 operations

Source: Internet
Author: User

You can play a specified MP3 file or an MP3 file in an embedded resource.

The following is the source code of the MP3 operation class I wrote:

 

 Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Using system. runtime. interopservices;

/*************************************** **************************************** **************************************** *************
** File Name:
** Statement:
** Creator: Huang Cong
** Creation date: 2009.10.8
** Modifier: Huang Cong
** Last modification date: 2009.10.8
**************************************** **************************************** **************************************** ************/
Namespace Tool
{
/*************************************** **************************************** **************************************** *********
** Class Name:
** Statement:
** Creator: Huang Cong
** Creation date: 2009.7.15
** Modifier: Huang Cong
** Last modification date: 2009.7.15
**************************************** **************************************** **************************************** ********/
Public class mp3player
{
# Region-Attribute-
[Dllimport ("winmm. dll")]
Static extern int32 mcisendstring (string command,
Stringbuilder buffer int32, buffersize, intptr hwndcallback );

/// <Summary>
/// Temporary music file storage
/// </Summary>
Private string m_musicpath = "";

/// <Summary>
/// Parent form handle
/// </Summary>
Private intptr m_handle;
# Endregion

# Region-constructor-
/// <Summary>
/// Create an MP3 playing class
/// </Summary>
/// <Param name = "Music"> embedded music file </param>
/// <Param name = "path"> Save path of the temporary music file </param>
/// <Param name = "handle"> parent form handle </param>
Public mp3player (byte [] Music, string path, intptr handle)
{
Try
{
M_handle = handle;
M_musicpath = path. Combine (path, "temptings ");
Filestream FS = new filestream (m_musicpath, filemode. Create );
FS. Write (music, 0, music. Length );
FS. Close ();
}
Catch (exception)
{

}
}

/// <Summary>
/// Create an MP3 playing class
/// </Summary>
/// <Param name = "musicpath"> path of the MP3 file to be played </param>
/// <Param name = "handle"> parent form handle </param>
Public mp3player (string musicpath, intptr handle)
{
M_musicpath = musicpath;
M_handle = handle;
}

Public mp3player (byte [] Music, intptr handle ):
This (music, @ "C: \ WINDOWS \", handle)
{

}

# Endregion

# Region-play music-
Public void open (string path)
{
If (path! = "")
{
Try
{
Mcisendstring ("open" + path + "alias Media", null, 0, m_handle );
Mcisendstring ("play media", null, 0, m_handle );
}
Catch (exception)
{

}
}
}

Public void open ()
{
Open (m_musicpath );
}
# Endregion

# Region-Stop playing music-
Void closemedia ()
{
Try
{
Mcisendstring ("close all", null, 0, m_handle );
}
Catch (exception)
{
}
}
# Endregion
}
}

 

1. Import the MP3 file to Vs and set it to an embedded Resource:

 

MP3 files

 

2. import to the resource. C # regards MP3 as the file type and stores it in byte [] format:

 

3. Set the MP3 file as an embedded Resource:

 

4. Create a new form project and call the MP3 class method in the load event to play the music:

 

Private void formmain_load (Object sender, eventargs E)
{// Load the music mp3player MP3 = new mp3player (properties. Resources. Music, handle );

// Start playing mp3.open ();
}

 

From: http://www.cnblogs.com/Leo_wl/archive/2010/05/27/1745637.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.