C # Use APIs to play MP3 and other audio files

Source: Internet
Author: User
C # No class is provided for playing audio files such as MP3. To write audio file programs such as MP3, you must use third-party controls or classes. This article uses the API function mcisendstring to compile a class for playing audio files such as MP3.

The source code is as follows:

1. Media playing class composed of mcisendstring using the API function.

Using system;
Using system. runtime. interopservices;
Using system. text;
Using system. IO;
Namespace clsmciplay
{
/// <Summary>
/// Summary of clsmci.
/// </Summary>
Public class clsmci
{
Public clsmci ()
{
//
// Todo: add the constructor logic here
//
}

// Define the string variable used by the API Function
[Financialas (unmanagedtype. byvaltstr, sizeconst = 260)]
Private string name = "";
[Financialas (unmanagedtype. byvaltstr, sizeconst = 128)]
Private string durlength = "";
[Financialas (unmanagedtype. lptstr, sizeconst = 128)]
Private string temstr = "";
Int ilong;
// Defines the enumerated variables of the playback status.
Public Enum state
{
Mplaying = 1,
Mpuase = 2,
Mstop = 3
};
// Structure Variable
Public struct structmci
{
Public bool bmut;
Public int idur;
Public int IPOs;
Public int ivol;
Public int ibal;
Public String INAME;
Public state;
};

Public structmci MC = new structmci ();

// Obtain the playback file attributes
Public String filename
{
Get
{
Return MC. INAME;
}
Set
{
// Asciiencoding ASC = new asciiencoding ();
Try
{
Temstr = "";
Temstr = temstr. padleft (127, convert. tochar (""));
Name = Name. padleft (260, convert. tochar (""));
MC. INAME = value;
Ilong = apiclass. getw.pathname (MC. INAME, name, name. Length );
Name = getcurrpath (name );
// Name = "open" + convert. tochar (34) + name + convert. tochar (34) + "alias Media ";
Name = "open" + convert. tochar (34) + name + convert. tochar (34) + "alias Media ";
Ilong = apiclass. mcisendstring ("close all", temstr, temstr. length, 0 );
Ilong = apiclass. mcisendstring (name, temstr, temstr. length, 0 );
Ilong = apiclass. mcisendstring ("set media time format milliseconds", temstr, temstr. length, 0 );
MC. State = state. mstop;
}
Catch
{
MessageBox. Show ("error! ");
}
}
}
// Play
Public void play ()
{
Temstr = "";
Temstr = temstr. padleft (127, convert. tochar (""));
Apiclass. mcisendstring ("play media", temstr, temstr. length, 0 );
MC. State = state. mplaying;
}
// Stop
Public void stopt ()
{
Temstr = "";
Temstr = temstr. padleft (128, convert. tochar (""));
Ilong = apiclass. mcisendstring ("close media", temstr, 128, 0 );
Ilong = apiclass. mcisendstring ("close all", temstr, 128, 0 );
MC. State = state. mstop;
}

Public void puase ()
{
Temstr = "";
Temstr = temstr. padleft (128, convert. tochar (""));
Ilong = apiclass. mcisendstring ("Pause Media", temstr, temstr. length, 0 );
MC. State = state. mpuase;
}
Private string getcurrpath (string name)
{
If (name. Length <1) Return "";
Name = Name. Trim ();
Name = Name. substring (0, name. Length-1 );
Return name;
}
// Total time
Public int duration
{
Get
{
Durlength = "";
Durlength = durlength. padleft (128, convert. tochar (""));
Apiclass. mcisendstring ("status media length", durlength, durlength. length, 0 );
Durlength = durlength. Trim ();
If (durlength = "") return 0;
Return (INT) (convert. todouble (durlength)/1000f );
}
}

// Current time
Public int currentposition
{
Get
{
Durlength = "";
Durlength = durlength. padleft (128, convert. tochar (""));
Apiclass. mcisendstring ("status Media Position", durlength, durlength. length, 0 );
MC. IPOs = (INT) (convert. todouble (durlength)/1000f );
Return MC. IPOs;
}
}
}

Public class apiclass
{
[Dllimport ("kernel32.dll", charset = charset. Auto)]
Public static extern int getaskpathname (
String lpszlongpath,
String delimiter file,
Int cchbuffer
);

[Dllimport ("winmm. dll", entrypoint = "mcisendstring", charset = charset. Auto)]
Public static extern int mcisendstring (
String lpstrcommand,
String lpstrreturnstring,
Int ureturnlength,
Int hwndcallback
);
}
}

 

Ii. Simple code used to test media playback:

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;
Using system. text;
Using system. IO;
Using clsmciplay;

Namespace mciplay
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private system. componentmodel. icontainer components;
Private system. Windows. Forms. Timer timer1;
Private system. Windows. Forms. Button play;
Private system. Windows. Forms. Button stop;
Private system. Windows. Forms. Button puase;
Private system. Windows. Forms. Label playfilename;
Private system. Windows. Forms. Label duration;
Private system. Windows. Forms. Label currentposition;
Private system. Windows. Forms. openfiledialog openfiledialog1;
Private system. Windows. Forms. Button browserfile;
Clsmci MP = new clsmci ();

Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. components = new system. componentmodel. Container ();
This. Play = new system. Windows. Forms. Button ();
This. playfilename = new system. Windows. Forms. Label ();
This. Duration = new system. Windows. Forms. Label ();
This. Stop = new system. Windows. Forms. Button ();
This. puase = new system. Windows. Forms. Button ();
This. currentposition = new system. Windows. Forms. Label ();
This. timer1 = new system. Windows. Forms. Timer (this. components );
This. openfiledialog1 = new system. Windows. Forms. openfiledialog ();
This. browserfile = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Play
//
This. Play. Location = new system. Drawing. Point (102,243 );
This. Play. Name = "play ";
This. Play. size = new system. Drawing. Size (78, 24 );
This. Play. tabindex = 0;
This. Play. Text = "play ";
This. Play. Click + = new system. eventhandler (this. play_click );
//
// Playfilename
//
This. playfilename. autosize = true;
This. playfilename. Location = new system. Drawing. Point (12, 15 );
This. playfilename. Name = "playfilename ";
This. playfilename. size = new system. Drawing. Size (0, 17 );
This. playfilename. tabindex = 1;
//
// Duration
//
This. Duration. autosize = true;
This. Duration. Location = new system. Drawing. Point (15, 51 );
This. Duration. Name = "duration ";
This. Duration. size = new system. Drawing. Size (0, 17 );
This. Duration. tabindex = 2;
//
// Stop
//
This. Stop. Location = new system. Drawing. Point (282,243 );
This. Stop. Name = "stop ";
This. Stop. size = new system. Drawing. Size (81, 24 );
This. Stop. tabindex = 3;
This. Stop. Text = "stop ";
This. Stop. Click + = new system. eventhandler (this. stop_click );
//
// Puase
//
This. puase. Location = new system. Drawing. Point (198,243 );
This. puase. Name = "puase ";
This. puase. size = new system. Drawing. Size (72, 24 );
This. puase. tabindex = 4;
This. puase. Text = "puase ";
This. puase. Click + = new system. eventhandler (this. puase_click );
//
// Currentposition
//
This. currentposition. autosize = true;
This. currentposition. Location = new system. Drawing. Point (15, 87 );
This. currentposition. Name = "currentposition ";
This. currentposition. size = new system. Drawing. Size (0, 17 );
This. currentposition. tabindex = 5;
//
// Timer1
//
This. timer1.enabled = true;
This. timer1.interval = 1000;
This. timer1.tick + = new system. eventhandler (this. timerw.tick );
//
// Browserfile
//
This. browserfile. Location = new system. Drawing. Point (312,165 );
This. browserfile. Name = "browserfile ";
This. browserfile. size = new system. Drawing. Size (87, 24 );
This. browserfile. tabindex = 6;
This. browserfile. Text = "selectfile ";
This. browserfile. Click + = new system. eventhandler (this. browserfile_click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (433,287 );
This. Controls. Add (this. browserfile );
This. Controls. Add (this. currentposition );
This. Controls. Add (this. puase );
This. Controls. Add (this. Stop );
This. Controls. Add (this. duration );
This. Controls. Add (this. playfilename );
This. Controls. Add (this. Play );
This. Name = "form1 ";
This. Text = "form1 ";
This. resumelayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

// Select MP3 file playback
Private void play_click (Object sender, system. eventargs E)
{
Try
{
MP. Play ();
}
Catch
{
MessageBox. Show ("error! ");
}
}

// Pause the playback.
Private void puase_click (Object sender, system. eventargs E)
{
Try
{
MP. puase ();
}
Catch
{
MessageBox. Show ("error! ");
}

}

// Stop playing
Private void stop_click (Object sender, system. eventargs E)
{
Try
{
MP. stopt ();
}
Catch
{
MessageBox. Show ("error! ");
}
}

// Display the playback progress once per second
Private void timerjavastick (Object sender, system. eventargs E)
{
Currentposition. Text = mp. currentposition. tostring ();
}

// Browse files
Private void browserfile_click (Object sender, system. eventargs E)
{
Try
{
Openfiledialog1.filter = "*. MP3 | *. MP3 ";
Openfiledialog1.filename = "";
If (openfiledialog1.showdialog () = dialogresult. OK)
{
MP. filename = openfiledialog1.filename;
Playfilename. Text = openfiledialog1.filename;
Duration. Text = mp. Duration. tostring ();
}
}
Catch
{
MessageBox. Show ("error! ");
}
}
}
}

This program is compiled in. NET 2003 and Win XP SP1.

Related Article

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.