Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Media Player With MCI

4.23/5 (10 votes)
15 Feb 2011CPOL2 min read 41.6K   4.1K  
A media player with winmm.DLL (MCI). Includes radio, search playlist, nd line up playlist (optional) features.

Image 1

Introduction

This article is about MCISENDSTRING and winmm.dll. I use them to play media files and this is a good sample about playing media files. I know you can find a lot of examples about MCI but not like this one. This sample project has a radio selection and you can edit it however you desire by adding radio URLs. I will improve its interface and will handle its bugs as and when I get time.

To find songs, use 'CTRL+F'. You can sort your playlist with 'CTRL+S' and play your order with 'S' or clean up your order with 'SHIFT+S'. In the 'Görsel Öğe' tab you can see the MP3 tags, media file length (-/+), your playlist's length (in minutes), and you can specify your interface by selecting the background and foreground colours. In the 'Seçenekler' tab, and you can choose how you want to listen to songs (random, repeat single song, repeat playlist, loop (at the end of the playlist)). And in this tab, you can save your list and in one click, you can get it all the time (see the listbox on the right). And of course, the radio section, I want you to explore it. :)

This solution helps you to learn MCISENDSTRING commands, and you should look at 'Player.cs'. Also, you can look in the 'Registry' region to learn the Regedit entries using C#, the 'Zaman' region to learn about MCI time commands like 'status media length', the 'MP3 ID3v1' region to learn about an MP3's ID3v1 information like 'Artist', 'Year', and the other regions to examine other functions in 'Player.cs'.

You can use any of my code parts in your projects. It will help you to learn about MCI , because almost all of code parts have explanations.

Using the code

Firs of all, we need the Player class to play media files because it has MCI commands in functions. The content is explained at the top. In the Mp3PLayer (Main Form) constructor, there are some similarities because almost all of the component functions are in Player.cs. The main similarity is Player muzik=new Player();. This is important. In the 'DragDrop' region, you can learn about drag drop (Desktop to WinForms, directory to WinForms, file to WinForms). Also, for interface changes like colour changes, look in the 'Skins' region.

Global variables and importing winmm.DLL:

C#
[DllImport("winmm.dll")] 
private static extern long mciSendString(string strKomut, 
        StringBuilder strDonenDeger, int mediaUzunluk, IntPtr hwndCallback);
//And I Used it in Functions
//It is a classic API using.

Time functions: The Open function is in 'Player.cs' is used to open media files to play with the MCI command strings.

SarkiUzunlukMiliSaniye(int sira)

C#
Open(lst.Items[sira].SubItems[1].Text);
komut = "status Sarki length";
mciSendString(komut, donendeger, donendeger.Capacity, IntPtr.Zero); //Media File LENGTH!
return Convert.ToInt32(donendeger.ToString());

Button press events: trcsarkiyer2 is a scrollbar to follow the media status and SarkiUzunlukMilisaniye is a 'status media file' function.

Play(int sira)

C#
Stop();
Open(lst.Items[sira].SubItems[1].Text); //Other Function Of MCI
trcsarkiyer2.Maximum = SarkiUzunlukMiliSaniye(sira); //ScrollBar For Position And Length
komut = "play Sarki";
mciSendString(komut, null, 0, IntPtr.Zero);
play = true;

History

This is my first article. This app might contain bugs. It is only intended as a learning tool. Happy coding ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)