달력

32024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

예전에 자바로 겁나 헤메던거 무지무지 간단하게 끝나네 그랴~


-- 출처 : http://www.codeproject.com/useritems/Audio_Player__with_Winmm.asp 


Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report this article.

Sample Image - Audio_Player__with_Winmm.jpg

Introduction

After looking for a simple, yet free, mp3 player I deceided to make my own. I based myself on a commercial package that shall remain nameless [ Let's just say it that package was expensive considering it was importing the winmm.dll ]  Almost every example I found for playing audio file [in my case MP3s] using the winmm.dll only had play/stop capabilities (and sometimes pause). Which is fine, I just wanted a bit more, and still keep it simple. Of course, if I had wanted a full blown MP3 player, I propably would have download one of the many free application out there. That wasn't my purpose.
So the code is fairly simple. It uses the winmm.dll from Windows to play,stop,pause etc. As well as control the volume of the left/right channels (if there are more then 1). Also, I had fun trying my hand at parsing XML, so the application gets audio file infomartion from a Windows Media Playlist [Making one is easy, just use Windows Media Player].

Just please keep in mind, that I'm not a programmer by trade and this is my first contribution to the CodeProject. I know that I'm not re-inventing the wheel here, just hoping this will help someone somewhere.

The project has a couple of files:
-Player.cs [Which has all of the winmm.dll string commands]
more can be found here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_multimedia_command_strings.asp
Which in my opinion, is a pain to understand sometimes.
-readPlaylist.cs
This opens up a open dialog window and helps you select a *.wpl file
-MainPlayer.cs
Basically the buttons and click events etc etc.

Let's look at the Player.cs
Tried putting as many relevant comments in the code as I code with out going over board like I'm doing writting this acticle.

Collapse
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace MP3player { class Player { private string Pcommand; private bool isOpen; [DllImport("winmm.dll")] private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, int bla); /// <SUMMARY> /// Not much to conctruct here /// </SUMMARY> public Player() { } /// <SUMMARY> /// Stops currently playing audio file /// </SUMMARY> public void Close() { Pcommand = "close MediaFile"; mciSendString(Pcommand, null, 0, 0); isOpen = false; } /// <SUMMARY> /// Opens audio file to play /// </SUMMARY> /// This is the audio file's path and filename public void Open(string sFileName) { Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile"; mciSendString(Pcommand, null, 0, 0); isOpen = true; } /// <SUMMARY> /// Plays selected audio file /// </SUMMARY> /// If True,audio file will repeat public void Play(bool loop) { if (isOpen) { Pcommand = "play MediaFile"; if (loop) Pcommand += " REPEAT"; mciSendString(Pcommand, null, 0, 0); } } /// <SUMMARY> /// Pauses currently playing audio file /// </SUMMARY> public void Pause() { Pcommand = "pause MediaFile"; mciSendString(Pcommand, null, 0, 0); } /// <SUMMARY> /// Returns the current status player: playing,paused,stopped etc. /// </SUMMARY> public string Status() { int i = 128; System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i); mciSendString("status MediaFile mode", stringBuilder, i, 0); return stringBuilder.ToString(); } /// <SUMMARY> /// Get/Set Lelf Volume Factor /// </SUMMARY> public int LeftVolume { get { return 0; //Guess could be used to return Volume level: I don't need it } set { mciSendString(string.Concat("setaudio MediaFile left volume to ", value), null, 0, 0); } } /// <SUMMARY> /// Get/Set Right Volume Factor /// </SUMMARY> public int RightVolume { get { return 0; //Guess could be used to return Volume level: I don't need it } set { mciSendString(string.Concat("setaudio MediaFile right volume to ", value), null, 0, 0); } } /// <SUMMARY> /// Get/Set Main Volume Factor /// </SUMMARY> public int MasterVolume { get { return 0; //Guess could be used to return Volume level: I don't need it } set { mciSendString(string.Concat("setaudio MediaFile volume to ", value), null, 0, 0); } } /// <SUMMARY> /// Get/Set Bass Volume Factor /// </SUMMARY> public int Bass { get { return 0; } set { mciSendString(string.Concat("setaudio MediaFile bass to ", value), null, 0, 0); } } /// <SUMMARY> /// Get/Set Treble Volume Factor /// </SUMMARY> public int Treble { get { return 0; } set { mciSendString(string.Concat("setaudio MediaFile treble to ", value), null, 0, 0); } } } } 

 

Now for reading the .wpl file to get MP3s [or whatever] to play
readPlaylist.cs

Collapse
using System; using System.Collections; using System.Text; using System.Xml; namespace MP3player { class readPlaylist { private ArrayList name = new ArrayList(); private string m_xmlFile; /// <SUMMARY> /// The Windows Media Playlist Path xxx.wpl file /// </SUMMARY> public string playListPath { get { return m_xmlFile; } set { m_xmlFile = value; Makeplaylist(); } } /// <SUMMARY> /// Return an Arraylist of file found in Windows Media Playlist file /// </SUMMARY> public ArrayList PlayList { get { return name; } } /// <SUMMARY> /// Fills up an Arraylist with titles found in the Windows Media Playlist file. /// Using XmlTextReader /// </SUMMARY> private void Makeplaylist() { XmlTextReader readList = new XmlTextReader(m_xmlFile); while (readList.Read()) { if (readList.NodeType == XmlNodeType.Element) { if (readList.LocalName.Equals("media")) { name.Add(readList.GetAttribute(0).ToString()); } } } } } }

Let's use the code now.
Here's a part of the file with all the click event and so on.
So here's the start, make sure you got all our references...

MediaPlayer.cs

Collapse
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MP3player { public partial class MainPlayer : Form { ArrayList nowPlaylist = new ArrayList(); Player pl = new Player(); public MainPlayer() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { lstBoxPlayList.Items.Clear(); openPlaylistDialog.ShowDialog(); readPlaylist readList = new readPlaylist(); readList.playListPath = openPlaylistDialog.FileName; nowPlaylist = readList.PlayList; for (int x = 0; x < nowPlaylist.Count; x++) { lstBoxPlayList.Items.Add(nowPlaylist[x]); } lstBoxPlayList.SetSelected(0, true); } 


The above code is pretty straight foward. The btnOpen_click event will open a OpenFileDialog window. Get the selected .wpl file and send it to the readPlaylist class, which will in turn parse through it return an Arraylist with all the file names and there paths. Once that done, loop it to display it's content [in this case a listbox]. Now you have all your files ready to be played, all that's needed is to select one and press play. Or doudle-click on it start it (I won't show that part here).

To play the selected file in the listbox:

 private void btnPlay_Click(object sender, EventArgs e) { if (lstBoxPlayList.Items.Count > 0) { pl.Open(lstBoxPlayList.SelectedItem.ToString()); pl.Play(false); } } 

A simple way of checking that a file is really selected before playing.
Besides that, pretty simple. Of course, if you've never done this, it's not. It's called the learning process.
As mentionned above, I think the comments in the code are pretty good. That and the MSDN link that's at the top... you should do fine.

Another thing, also tried my hand at controlling the Bass/Treble. Don't know if it works, since my speaker system on my computer ain't great, and I'm hard of hearing [hence the reason my speaker system is crappy]. But if I read the MSDN right, it seems to work the same way as the volume control.

Guess that's about it, hope this little example will help someone out.

About loneferret


Studied English Teaching as a second language at university here in montreal(you wouldn't know it the way I type would ya), after that did some networking course with a bit of programming. Mostly working as a network admin these days.
I also like to write short stories, and draw..mostly black and white stuff...

Click here to view loneferret's online profile.

Posted by tornado
|