Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
Windows Programming > play mp3, wav, wmv, mpg, avi etc files in win32 api program

Play au, snd, mp3, wav, wmv, asf, wma, mpg, midi, rmi, vob, dat files in Win32 API Program
Playing any video or audio such as au, snd, mp3, wav, wmv, asf, wma, mpg, midi, rmi, vob, or dat files in win32 api program is very simple, when you come to see the code you will come to believe, that how easy it is using MCI.

Your program must include windows.h as we as link to winmm library. Under mingw it is named libwinmm.a and under other compilers it is winmm.lib.


What you do basically when playing an audio file is:
1. open the audio file
2. play / pause / resume / stop
3. close the audio file

All of above mentioned actions are performed by sending properly formated string to MCI device, by using function mciSendString.
a) A very simple example would be as below, which will play song.mp3 from current folder your program executable is in:


//load audio and (in short) name it 'myFile'
mciSendString("open song.mp3 type mpegvideo alias myFile", NULL, 0, 0);
//play audio
mciSendString("play myFile", NULL, 0, 0);
//close all
mciSendString("close myFile", NULL, 0, 0);


Where myFile is a special textual word we have used to represent the auio file, so that we do not need to repeat the file name again in every action, becuase we may have long filename alognwith folder path such as
C:\Documents and Settings\User1\Desktop\Sounds\song.mp3

NOTE: Long file names which include space character must be enclosed within double quotes, such as:
mciSendString("open \"C:\Documents and Settings\User1\Desktop\Sounds\song.mp3\" type mpegvideo alias myFile", NULL, 0, 0);


Other than that, you can also format many more command to send to MCI device.

a) Playing from specific position:
mciSendString("play myFile from 115", NULL, 0, 0);

b) Stop playing and reset MCI device for current file:
mciSendString("stop myFile", NULL, 0, 0);

c) Pause the play:
mciSendString("pause myFile", NULL, 0, 0);

d) Resume the paused audio:
mciSendString("resume myFile", NULL, 0, 0);

e) Repeated play
mciSendString("play myFile repeat", NULL, 0, 0);


More help on command string can be viewed here:
http://msdn2.microsoft.com/en-us/library/ms712587(VS.85).aspx

I am sure this is useful to you. You can use this procedure to play any windows supported audio / midi / video formats.

NOTE:When playing video files you have to establish control of the video display window, By default a new window is created for your video.


Enjoy making your own mp3 players ;).