Introduction
Play MP3 files and Movies from your application
Description
To play mp3 files or movies in your application , you can use the
ActiveMovie ActiveX control. To insert that in your project, select
Project-->
Add to project-->
Components and Controls
from the menu of Visual Studio. In the dialog box that opens, select
"Registered ActiveX Controls".In that folder, you can see a control
called "ActiveMovieControl Object" and select the Insert button.
Once you have added it to your project, you can use it to play your audio
and video files.To do that you have to follow the following steps.
Note : The ActiveMovie control is actually the Windows media player
in disguise.
STEP 1 to STEP 5 are absolutely essential. STEPS 6 to 9 are optional
which you can use as per your requirements.
Implementation
Step 1
In your stdafx.h file, add the following line.
#include "ActiveMovie3.h"
Step 2
Use the following line to define a value for the player
#define ID_PLAYER 10001
Step 3
To open a file , you can display an open file common dialog using the following code.
static char BASED_CODE szFilter[] = "MP3 Files(*.mp3)|*.mp3||";
CFileDialog dlgFileOpen(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,NULL);
// Show the dialog
dlgFileOpen.DoModal();
// Get the Selected path and file name
CString strFileName;
strFileName = dlgFileOpen.GetPathName();
// Create the control in your application
CRect rcClient;
GetClientRect(&rcClient);
m_ActiveMovie.Create("MP3",0,CRect(0,0,rcClient.Width(),25),this,ID_PLAYER,NULL,FALSE);
m_ActiveMovie.ShowWindow(SW_SHOW);
Note : If you dont want to show the player and if you only want to run
the audio file, then use m_ActiveMovie.ShowWindow(SW_HIDE);
instead of m_ActiveMovie.ShowWindow(SW_SHOW);
Step 4
To set the file name and start it, use the following code
m_ActiveMovie.SetFileName(strFileName);
m_ActiveMovie.SetAutoStart(TRUE);
Instead of using SetAutoStart, you can use m_ActiveMovie.Run(); too.
SetAutoStart automatically starts playing the selected file.
Step 5
To stop or pause the output,
m_ActiveMovie.Stop();
m_ActiveMovie.Pause();
After calling pause, you can call m_ActiveMovie.Run(); to play the file again.
Properties
The following steps are optional. They can be used as per your requirements.
// To enable tracking tooltips, use this line of code
m_ActiveMovie.EnableTrackingToolTips(TRUE);
// To allow the user to hide your player window, you can use this line
m_ActiveMovie.SetAllowHideDisplay(TRUE);
// If the player is visible, then you can use this line of code to hide only
// the controls of the player
m_ActiveMovie.SetAllowHideControls(TRUE);
// If you want your movie or audio to restart after it has finished playing fully,
// then you can use this line of code
m_ActiveMovie.SetAutoRewind(TRUE);
// If you want the user to change the display mode of your player, then you can
// add this statement
m_ActiveMovie.SetAllowChangeDisplayMode(TRUE);
// To show the controls , you can use
m_ActiveMovie.SetShowControls(TRUE);
// To show or hide the position controls , you can use either TRUE or FALSE for
// the SetShowPositionControls function
m_ActiveMovie.SetShowPositionControls(TRUE);
Colour
The following steps are optional too. Use them as you please.
// You can see that the default player colour is Black. To change it, you can use
// the following line of code where you can specify any value of RGB for the parameter.
// I have specified Blue here.
m_ActiveMovie.SetDisplayBackColor(unsigned long (RGB(0,0,200)));
// You can see that the default text colour is White. To change it, you can use
// the following line of code where you can specify any value of RGB for the parameter.
// I have specified Yellow here.
m_ActiveMovie.SetDisplayForeColor(unsigned long (RGB(255,255,0)));
Balance and Volume
To set the balance between the left and the right speakers, you can add a
slider control and set its range from -10,000 to 10,000, get its value
and use the SetBalance function to set that property.
A value of -10,000 indicates that the output would be only for your Left speaker
A value of 10,000 indicates that the output would be only for your Right speaker
A value of 0 indicates that the output would be balanced between the right & left speakers
Sample code for setting the balance to the Left speaker :-
long lBalance;
lBalance = -10000; // Left Speaker
m_ActiveMovie.SetBalance(lBalance);
To set the volume level, you can use the SetVolume function.
A value of -10,000 indicates that the volume level is zero.
A value greater that than can be used to change the volume level.
The maximum value for this function is Zero which means that the volume level
is at its maximum.
Sample code for setting the maximum volume :-
long lVolume;
lVolume = 0; // Maximum volume
m_ActiveMovie.SetVolume(lVolume);
Duration and Playback
To get the playing duration of the file, you can use the following snippet of code.
int nValue;
nValue = m_ActiveMovie.GetDuration();
int nHours,nMins,nSeconds;
nHours = nValue/3600;
int nTemp;
nTemp = nValue%3600;
if(nHours<=0)
{
nMins = nValue/60;
nSeconds = nValue%60;
}
else
{
nMins = nTemp/60;
nSeconds = nTemp%60;
}
CString strDuration;
if(nHours<=0)
{
if(nMins<=0)
{
strDuration.Format("Duration : %d Seconds",nSeconds);
}
else
{
strDuration.Format("Duration : %d Minutes and %d Seconds",nMins,nSeconds);
}
}
else
{
strDuration.Format("Duration : %d Hours %d Minutes and %d Seconds",
nHours,nMins,nSeconds);
}
SetWindowText(strDuration);
Here, i am setting the duration time as the title of the window.
You can display it on a label or do whatever you want with strDuration.
To get and set the position of the file, you can use the SetCurrentPosition
and GetCurrentPosition functions.
Conclusion
Thats it pals. The most commonly used functionalities have been covered here.
Usage :
You might be wondering what to do with this now. If you want a list of possible usages, here they are.
1) To build your own skinned player,
2) To override the default action of windows media player,
3) To customise windows media player by adding,removing or showing the buttons that you want to,
4) To play mp3 audio from your project,
5) To show a movie in your dialog, etc.etc.etc.
The possibilities are endless and its upto you. Try to devise as many usages as possible for this
control.All luck and have a great time.
|