VB.net and media player9.0 embedded development

Source: Internet
Author: User

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:
  1. Private sub button#click (byval sender as system. object,
  2. Byval e as system. eventargs) handles button1.click
  3. 'Open and close all optical drive warehouse doorways
  4. Dim I
  5. Dim K = axwindowsmediaplayer1.cdromcollection. Count ()
  6. If K> 1 then
  7. For I = 0 to k-1
  8. Axwindowsmediaplayer1.cdromcollection. Item (I). Eject ()
  9. Next
  10. Else
  11. Axwindowsmediaplayer1.cdromcollection. Item (0). Eject ()
  12. End if
  13. End sub
  14. Private sub form1_load (byval sender as object,
  15. Byval e as system. eventargs) handles mybase. Load
  16. 'The number of optical drives and their drive letters in the computer
  17. Dim I
  18. Dim cdlabel as string
  19. Dim K = axwindowsmediaplayer1.cdromcollection. Count ()
  20. If K> 1 then
  21. For I = 0 to k-1
  22. Cdlabel = cdlabel &
  23. Axwindowsmediaplayer1.cdromcollection. Item (I). drivespecifier ()
  24. Label1.text = "Total computers" & K &
  25. "CD-ROM" & "are" & cdlabel &""
  26. Next
  27. Else
  28. Cdlabel = cdlabel +
  29. Axwindowsmediaplayer1.cdromcollection. Item (0). drivespecifier ()
  30. Label1.text = "Total computers" & K &
  31. "CD-ROM" & "are" & cdlabel &""
  32. End if
  33. End sub
  34. Private sub button2_click (byval sender as system. object,
  35. Byval e as system. eventargs) handles button2.click
  36. Dim strfilename as string
  37. Dim openfiledialog1 as system. Windows. Forms. openfiledialog =
  38. New system. Windows. Forms. openfiledialog ()
  39. Openfiledialog1.showdialog ()
  40. Strfilename = openfiledialog1.filename
  41. Axwindowsmediaplayer1.url = strfilename
  42. End sub
  43. Private sub button3_click (byval sender as system. object,
  44. Byval e as system. eventargs) handles button3.click
  45. Axwindowsmediaplayer1.controls. Play ()'
  46. End sub
  47. Private sub button5_click (byval sender as system. object,
  48. Byval e as system. eventargs) handles button5.click
  49. Axwindowsmediaplayer1.controls. Stop () 'Stop
  50. End sub
  51. Private sub axwindowsmediaplayer=playstatechange (byval sender as object,
  52. Byval e as axmicrosoft. mediaplayer. InterOP. _ wmpocxevents_playstatechangeevent) handles
  53. Axwindowsmediaplayer1.playstatechange
  54. 'Indicates this event is triggered when the playing media changes.
  55. Label4.text = "the total playback time of this file is "&
  56. Axwindowsmediaplayer1.currentmedia. durationstring
  57. Timer1.enabled = true
  58. Label5.text = "title "&
  59. Axwindowsmediaplayer1.currentmedia. getiteminfobytype ("title", "", 0)
  60. Label6.text = "author "&
  61. Axwindowsmediaplayer1.currentmedia. getiteminfobytype ("author", "", 0)
  62. End sub
  63. Private sub button4_click (byval sender as system. object,
  64. Byval e as system. eventargs) handles button4.click
  65. Axwindowsmediaplayer1.controls. Pause () 'pause
  66. End sub
  67. Private sub timereffectick (byval sender as system. object,
  68. Byval e as system. eventargs) handles timer1.tick
  69. Label2.text = "playback time "&
  70. CINT (axwindowsmediaplayer1.controls. currentposition) & "seconds"
  71. End sub
  72. Private sub trackbar1_scroll (byval sender as system. object,
  73. Byval e as system. eventargs) handles trackbar1.scroll
  74. Axwindowsmediaplayer1.settings. volume = trackbar1.value 'adjust the output volume
  75. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.