11 out of 18 rated this helpful - Rate this topic

Creating the Windows Media Player Control Programmatically

When you add the Windows Media Player control to a form from the Toolbox, an object of the class AxWMPLib.AxWindowsMediaPlayer is created. This wrapper class gives the Player all the functionality of an ActiveX control, including access to UI properties such as Location and Size.

If you do not require the properties exposed by AxWindowsMediaPlayer, or if your application does not have a graphical user interface, you can create a Player control programmatically. In this case, you create an object of the WMPLib.WindowsMediaPlayer class.

Note  Because the WindowsMediaPlayer object is not wrapped as an ActiveX control, it does not have any properties inherited from System.Windows.Forms.Control. As a result, the Controls property is not renamed to CtlControls, as it is in AxWindowsMediaPlayer.

To create the Windows Media Player control programmatically, you must first add a reference to wmp.dll, which is found in the \Windows\system32 folder. Adding this reference creates WMPLib.dll in your project folder, and a reference to WMPLib appears in Solution Explorer.

The following example code, part of a Form1 class, shows how to create a Player object and play a file. When playback ends, or if the file cannot be played, the form is closed.



' [ Visual Basic ]
Dim WithEvents Player As WMPLib.WindowsMediaPlayer

Private Sub PlayFile(ByVal url As String)
    Player = New WMPLib.WindowsMediaPlayer
    Player.URL = url
    Player.controls.play()
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                       Handles MyBase.Load
    ' TODO  Insert a valid path in the line below.
    PlayFile("c:\media\myaudio.wma")
End Sub

Private Sub Player_MediaError(ByVal pMediaObject As Object) _
                              Handles Player.MediaError
    MessageBox.Show("Cannot play media file.")
    Me.Close()
End Sub

Private Sub Player_PlayStateChange(ByVal NewState As Integer) _
                                   Handles Player.PlayStateChange
    If NewState = WMPLib.WMPPlayState.wmppsStopped Then
        Me.Close()
    End If
End Sub





// [ C# ]
WMPLib.WindowsMediaPlayer Player;

private void PlayFile(String url)
{
    Player = new WMPLib.WindowsMediaPlayer();
    Player.PlayStateChange += 
        new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
    Player.MediaError += 
        new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
    Player.URL = url;
    Player.controls.play();
}

private void Form1_Load(object sender, System.EventArgs e)
{
    // TODO  Insert a valid path in the line below.
    PlayFile(@"c:\myaudio.wma");
}

private void Player_PlayStateChange(int NewState)
{
    if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
    {
        this.Close();
    }
}

private void Player_MediaError(object pMediaObject)
{
    MessageBox.Show("Cannot play media file.");
    this.Close();
}




Related topics

Embedding the Windows Media Player Control in a .NET Framework Solution

 

 

Send comments about this topic to Microsoft

Build date: 2/15/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Incorrect code example - correction shown below

Hi guys,

When you get time, you might want to change the method from:

private void Player_PlayStateChange(int NewState) <--Um, no.
{
    if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
    {
        this.Close();
    }
}

TO:

       

        void axWMPlayer_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e) // yes!
        {
            if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsStopped)
    {

this.Close();
    }
        }

Cheers,

-Mike

Now we need control over it's audio output.
What i'm really interested in, is full control over an applications audio out device. This is important since I have multiple screens in different spots running different applications and ALL sounds emitted from that app needs to be played on the screen's own speakers, and not the system defaults one. $0$0 $0 $0I know that a lot of users just have one set of speakers, or at least only use one set at a time, but I have even programmed a virtual sound card, with 4 speakers, that does what i should, but I have to choose one as default, that's not what I want.$0 $0$0 $0 $0In C# it should be a simple option "Application.AudioOutDevice" Bamm... Change that and you change your applications audio output, and only yours. In XP we could select one output as default, then start the first application, then we could select another output as default and start a second program, but, you were so kind to make it automatically change output to the default output device on all running application.$0 $0$0 $0 $0My current solution is: running multiple instances of Windows Media Player, changing it's default audio out in regedit before it starts, luckly enough, that has to be restarted before it changes to another device... $0 $0$0 $0 $0This is where my long message actually makes sense to this guide.$0 $0Windows Media Player can change audio out, but why can't we change the audio out of ActiveX. Some of my content is DRM Protected, so i can't play that with my own directshow player...$0 $0$0 $0 $0I know this is possible, somehow, I've read about programs like Audials, they use ActiveX MediaPlayer to play DRM Protected audio on a virtual sound card, then recording it again, saving it as unprotected MP3's. That's not what I want, technically it's the same.$0 $0$0 $0 $0I know this is not really a place to get an answer, but maybe you would decide to open up for your low level audio api's and tell us how to use them, so programs like this can be made.$0