Windows Media Player 9.0 is the latest digital media player launched by Microsoft, which has added many practical features in addition to the excellence of Media Player 8.0. However, these features are not the focus of this article, this article is about how to use Media Player to write a own embedded software, and let it have playback Cd,vcd,avi,mp3 and so on.
To write this program you should first familiarize yourself with vb.net, because this program is written using vb.net. To install Windows Media Player 9.0 on your computer. Finally, the key is to install the Windows Mendia Player 9.0 SDK, which includes a DLL in the SDK that completes the conversion between COM and. NET, because Media Player 9.0 is programmed to use a media player 9.0 ActiveX controls, and the nature of ActiveX is a COM. The Windows Mendia Player 9.0 SDK can be downloaded to Microsoft MSDN.
After the installation of the above 3 software is not ready to write code, because to ensure that Mendia player 9.0 to communicate with the. NET Framework, you must also register Mendia Player 9.0 pia, the so-called PIA is "Primary Interop assemblies "The abbreviation himself translates it as" the original internal assembler set, "because COM technology was born before. NET technology to make COM and. NET communications need to be a middle tier to connect both sides use the Mendia Player 9.0 SDK in Mendia Player 9.0 Wmppia.dll in the Redist directory to register the PIA for Mendia player 9.0, enter in the command window: [path]regasm c:\WMSDK\WMPSDK9\ redist\ Wmppia.dll (RegAsm is the PIA registration program provided by. NET, path is its route, and you can find it using the search function).
You don't think you can program by registering a PIA, you have to add it to the global cache. In the Command window, enter: [path]gacutil/i C:\WMSDK\WMPSDK9\redist\wmppia.dll (Gacutil is the global cache add-in provided by. NET, path is its route, you can find it with search capabilities)
Once you've completed the 5 steps above, you can start writing your code. In order for. NET to use the PIA, we have to add the following statement at the beginning of the code: the Imports Microsoft.MediaPlayer.Interop can now start our programming journey.
Right-click the Vs.net IDE's Control Toolbox, select Customize Toolbox from the shortcut menu, choose Windows Media Player in the custom toolbox, and click OK (Figure 1)
Figure 1
A Mendia control flag is added at the bottom of the control tool stop. Double-click the control to place it in the window
Body. Right click on it and click "Properties" to set "select Mode" to "none" in the Properties window that appears (Figure 2) and click OK to remove the original playback control.
Figure 2
Then in the window, in the Add control like Figure 3, the maximum and minimum values for the TRACKBAR1 control are set to 100 and 0 in the Properties window, because media has a volume range of 0 to 100, and we use the TRACKBAR1 control to adjust the playback volume. The interval of T Imer1 is set to 1000.
Figure 3
You can now enter the following code:
Private Sub button1_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button1.Click
' Open and close all optical drive cartridge doors
Dim I
Dim k = AxWindowsMediaPlayer1.cdromCollection.count ()
If k > 1 Then
For i = 0 to K-1
AxWindowsMediaPlayer1.cdromCollection.Item (i). Eject ()
Next
Else
AxWindowsMediaPlayer1.cdromCollection.Item (0). Eject ()
End If
End Sub
Private Sub Form1_Load (ByVal sender as Object,
ByVal e as System.EventArgs) Handles MyBase.Load
' Get how many optical drives and their disk characters are in the computer
Dim I
Dim Cdlabel as String
Dim k = AxWindowsMediaPlayer1.cdromCollection.count ()
If k > 1 Then
For i = 0 to K-1
Cdlabel = Cdlabel &
AxWindowsMediaPlayer1.cdromCollection.Item (i). Drivespecifier ()
Label1.Text = "This computer has a total of" & K &
"Taiwan CD-ROM" & "& Cdlabel &", respectively.
Next
Else
Cdlabel = Cdlabel +
AxWindowsMediaPlayer1.cdromCollection.Item (0). Drivespecifier ()
Label1.Text = "This computer has a total of" & K &
"Taiwan CD-ROM" & "& Cdlabel &", respectively.
End If
End Sub
Private Sub button2_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button2.click
Dim strFileName as String
Dim OpenFileDialog1 as System.Windows.Forms.OpenFileDialog =
New System.Windows.Forms.OpenFileDialog ()
Openfiledialog1.showdialog ()
strFileName = Openfiledialog1.filename
Axwindowsmediaplayer1.url = strFileName
End Sub
Private Sub button3_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button3.click
AxWindowsMediaPlayer1.controls.play () ' Play
End Sub
Private Sub button5_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button5.click
AxWindowsMediaPlayer1.controls.stop () ' Stop
End Sub
Private Sub Axwindowsmediaplayer1_playstatechange (ByVal sender as Object,
ByVal e as Axmicrosoft.mediaplayer.interop._wmpocxevents_playstatechangeevent) Handles
Axwindowsmediaplayer1.playstatechange
' Indicates that this event is triggered when the playback media has changed
Label4.text = "The total time this file plays is" &
AxWindowsMediaPlayer1.currentMedia.durationString
timer1.enabled = True
Label5.text = "title" &
AxWindowsMediaPlayer1.currentMedia.getItemInfoBytype ("Title", "" ", 0)
Label6.text = "Author" &
AxWindowsMediaPlayer1.currentMedia.getItemInfoBytype ("Author", "" ", 0)
End Sub
Private Sub button4_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button4.click
AxWindowsMediaPlayer1.controls.pause () ' Suspend
End Sub
Private Sub Timer1_Tick (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Timer1.tick
Label2.Text = "Played Time" &
CInt (AxWindowsMediaPlayer1.controls.currentPosition) & "seconds"
End Sub
Private Sub Trackbar1_scroll (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Trackbar1.scroll
AxWindowsMediaPlayer1.settings.volume = Trackbar1.value ' Adjust output volume
End Sub
Program Description: When you press F5 to start compiling, the system prompts you for an error, which is caused by the statement "Me.AxWindowsMediaPlayer1.enabled = True" In the code generated by the Windows Forms Designer. The reason for this is that the Media Player 9.0 control is compatible with. NET, and this issue has been confirmed by Microsoft, and the next release, Vs2003.net, will not have this problem, and in this release the solution is to remove the code.
The Getiteminfobytype attribute used in this article is used to read the information in the metadata (see the SDK for the definition of metadata), which is entered when the media is established, but this attribute does not automatically appear when you write the code, there is no intelligent fill function, The reason for this is that some of the new features of the Media Player 9.0 control are published through an interface and are not published in the COM type library, so they do not have the smart fill feature. However, you can write according to the syntax given by the SDK and the compiler will not recognize it. The unit in the program that plays the time of the file is seconds, and if you are interested you can convert it to a MM:SS format.
Run as shown in Figure 4
Figure 4