Windows Media Player 9.0 is the latest Digital Media Player released by Microsoft. It not only inherits the excellence of Media Player 8.0, but also adds many practical functions. However, these functions are not the focus of this article. This article focuses on how to use media player to compile an embedded software and enable it to play CD, VCD, Avi, MP3 and other functions.
To write this program, you must first familiarize yourself with VB.net, because this program is written using VB.net. Install Windows Media Player 9.0 on your computer. Finally, the most important thing is to install the Windows mendia Player 9.0 SDK, which contains a DLL. The function of this DLL is to complete COM and. net, because the Media Player 9.0 programming requires a Media Player 9.0 ActiveX control, and ActiveX is essentially a COM. For Windows mendia Player 9.0 SDK, you can download it from Microsoft's msdn.
You cannot write code after installing the above three software, because you must ensure that mendia Player 9.0 and. net Framework communications, you must also register mendia Player 9.0 Pia, the so-called Pia is the abbreviation of "primary InterOP assemblies" I translated it as "original internal assembly set ", the reason why we need to register it first is that COM technology was born in. before net technology. net Communication requires an intermediate layer to connect the two sides. In mendia Player 9.0, wmppia under the mendia Player 9.0 SDK redist directory is used. DLL to register PIA of mendia Player 9.0. In the command window, enter [path] regasm C: wmsdkwmpsdk9 redistwmppia. DLL (regasm is.. NET provides the PIA registration program. Path is its path. You can use the search function to find it ).
Don't think you can program Pia after you register it. You have to add it to the global cache. In the command window, enter [path] gacutil/I c: wmsdkwmpsdk9redistwmppia. DLL (gacutil is.. NET provides a global cache adding program. Path is its path. You can use the search function to find it)
After completing the five steps above, you can start coding. To enable. Net to use Pia, we must add the following statement at the beginning of the Code: Imports Microsoft. mediaplayer. InterOP. Now we can start our programming journey.
Right-click the control toolbox of vs.net IDE, and click "Custom toolbox" in the shortcut menu ", select "Windows Media Player" in "Custom toolbox" and click "OK" (figure 1)
Figure 1
A mendia control flag is added at the bottom of the control tool. Double-click the control and place it in the window.
Body. Right-click it and click "properties". In the displayed Properties window (Figure 2), set "select mode" to "NONE" and click "OK ", the purpose of this operation is to remove the original playback control.
Figure 2
Then add the 3 Control in the window. In the Properties window, set the maximum and minimum values of the trackbar1 control to 100 and 0, because the media volume range is 0 to 100, we use the trackbar1 control to adjust the playback volume. Set interval of timer1 to 1000.
Figure 3
Enter the following code:
Code:
- Private sub button#click (byval sender as system. object,
- Byval e as system. eventargs) handles button1.click
- 'Open and close all optical drive warehouse doorways
- 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
- 'The number of optical drives and their drive letters 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 = "Total computers" & K &
- "CD-ROM" & "are" & cdlabel &""
- Next
- Else
- Cdlabel = cdlabel +
- Axwindowsmediaplayer1.cdromcollection. Item (0). drivespecifier ()
- Label1.text = "Total computers" & K &
- "CD-ROM" & "are" & cdlabel &""
- 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 ()'
- 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 axwindowsmediaplayer=playstatechange (byval sender as object,
- Byval e as axmicrosoft. mediaplayer. InterOP. _ wmpocxevents_playstatechangeevent) handles
- Axwindowsmediaplayer1.playstatechange
- 'Indicates this event is triggered when the playing media changes.
- Label4.text = "the total playback time of this file 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 () 'pause
- End sub
- Private sub timereffectick (byval sender as system. object,
- Byval e as system. eventargs) handles timer1.tick
- Label2.text = "playback 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 the output volume
- End sub
Program Description: When you press F5 to start compilation, the system will prompt you with an error, which is "me" in "code generated by Windows Form Designer. axwindowsmediaplayer1.enabled = true "is caused by Media Player 9.0 controls and. net compatibility. This problem has been confirmed by Microsoft. In the next version, vs2003.net will not have this problem. In this version, the solution is to remove this code.
The getiteminfobytype attribute used in this article is used to read the information in the metadata (for the definition of metadata, see SDK). This information is input when the media is created, however, this attribute does not automatically appear when you write code. That is to say, it does not have the smart filling function. The reason is that some new features of the Media Player 9.0 control are released through interfaces, it is not released in the com Type Library, so it does not have the smart filling function. However, you can follow the syntax provided by the SDK, And the compiler will not recognize it. The Unit for calculating the playback time of a file in the program is seconds. If you are interested, convert it to the MM: SS format.
Run 4