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 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 );
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.