2_ 바삭바삭 프로그래밍/C# and Visual C++
[C#] MP3 재생 프로그램 - Playing MP3 files with C#
준환이형님_
2011. 7. 26. 14:20
Playing MP3 songs using mciSendString function
참고출처 : http://forum.codecall.net/csharp-tutorials/20420-tutorial-playing-mp3-files-c.html
갈수록 빠져드는 C#의 매력! ㅋ
C#은 사용이 기본적으로 어렵지 않을 뿐 아니라 라이브러리가 강력하다는 장점이 있습니다. 그리고 상황에 따라서는 순수 WinAPI dll을 가져올 수도 있죠.
오늘의 예제는 ArekBulski님(러시아분?)이 오픈소스 사이트(http://forum.codecall.net)에 올리신 예제입니다.
임포트하구요
using System.Runtime.InteropServices;
다음 폼과 같이 만듦니다. 텍스트창 하나, 버튼 3
Browse(불러오기)버튼을 아래코드로 채워줍니다.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FilenameTextbox.Text = openFileDialog1.FileName;
button3.Enabled = true; //So you dont play no file. lol
}
그런다음 메인 폼 클래스에 DLL을 읽어오기 위한 P/Invoke 정의를 붙이고
[DllImport("winmm.dll")] private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
버튼 3은 재생
private void button3_Click(object sender, EventArgs e) { mciSendString("open \"" + FilenameTextbox.Text + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero); mciSendString("play MediaFile", null, 0, IntPtr.Zero); button3.Enabled = false; button2.Enabled = true; }
버튼 2는 중지 코드를 입력 해 줍니다.
private void button2_Click(object sender, EventArgs e) { mciSendString("close MediaFile", null, 0, IntPtr.Zero); button2.Enabled = false; button3.Enabled = true; }
mp3 재생이 이렇게 쉽다니 ... ㅇㅅㅇ