C # Media Player Control)

Source: Internet
Author: User
Tags new set shuffle

To use the media player control, follow these steps:
1. Add this control to the control box: WMP. dll
2. Drag controls to the form
3. WMP. url = xxxx; WMP. Play.
Normally, the application is OK in these three steps. However, if I want to write a player, I have encountered some problems.
When using a non-DOTNET-based control in DOTNET, you need to do some additional things, but these things vs has already helped us do well. When we drag this control into the form, vs will automatically call aximp.exe and use it to generate a new set of Assembly packaged with the axhost class based on the original DLL or ocx (for more precise descriptions, see msdn). Therefore, according to wmplib. DLL generates two files: axinterop. wmplib. DLL, InterOP. wmplib. DLL. These two files contain converted classes. The encapsulated control inherits from system. Windows. Forms. axhost.
This is the first question. How do I use controls? It is very easy to drag controls, and can be effectively used in any way. However, what if I manually create a new control instance? That's not the case.
I have done various experiments and come to the following conclusions:
You cannot create an instance with a UI control in a non-Visualized Class, or you cannot add an instance to a visualized container.
I tested the following code:
Axwmplib. axwindowsmediaplayer WMP = new axwmplib. axwindowsmediaplayer ();
This. Controls. Add (WMP );
WMP. url = "appointment criteria ";
WMP. ctlcontrols. Play ();
These lines of code may not be able to run. It is found that if these lines of code are written in the form constructor, even if the show Code cannot be run, it is only written in the event after load or load, and show the window to run, otherwise an error will be thrown: The triggering type is "system. windows. forms. axhost + invalidactivexstateexception "exception. Some people have encountered this problem on the Internet, but they haven't even talked about the idea. Sorry.
Isn't it possible to run without show? I dragged a player control onto the form and wrote a line of play code after initializecomponent to run it. It can be seen that there is something strange in the middle. I compared the above Code with the code generated by dragging it up. There are two differences:
1. The code generated by the Drag Control is multiple (system. componentmodel. isupportinitialize) (This. wmp1). begininit ()/endinit ()
2. The Drag Control has one more row: This. wmp1.ocxstate = (system. windows. forms. axhost. state) (resources. getObject ("wmp1.ocxstate"); I checked msdn and ocxstate is the control state. Used to persist the status of the control. The status information of the COM component is written in the resource file of the corresponding form. You can use NotePad to open the form resource file. During manual creation, this ocxstate cannot be assigned a value.
I tried to add begininit and endinit, Which is useless. The problem is that the control status is incorrect. No way.

Obtain the information of the current playback media, which is very simple and can be obtained by currentmedia. How to Create a playlist? Let the player play the specified media in sequence? There is a way for me to find out:
Wmp1.currentplaylist. appenditem (wmp1.newmedia ("approximate parameters "))
I don't know how to use currentplaylist. I don't think it is necessary to implement a playlist on my own.
Very depressing. When you use WMP To Play files that are not supported by default such as rmvb, an error message box will pop up. You can use the WMP attribute as much as possible. settings. enableerrordialogs = false control it does not display, but there is still a sequent, that is, to call WMP. ctlcontrols. play (), it does not play, but click the triangle button in the control bar to play. Depressed.
I went to MPs. dll of storm 3 again. Sure enough, it uses the kernel of cool audio and video. However, the control provided by the hot weather is really bad. There are no advanced functions. Depressed. Or WMP. Continue to study WMP. I installed wmp11 now.
Now we have found some more comprehensive information about WMP APIs, as shown below:
Attribute/Method Name Description:
Detailed API documentation (more thorough than msdn ):
[Basic attributes]
URL: string; specifies the media location, local or network address
Uimode: string; player interface mode, which can be full, Mini, none, invisible (Case Insensitive)
Playstate: integer; playback status. The playstatechange event and statechange event are triggered when this attribute is changed. The value range is enumerated: wmplib. wmpplaystate. Its members are as follows:
Wmppsundefined = 0; // Unknown Status
Wmppsstopped = 1; // The playback stops.
Wmppspaused = 2; // pause
Wmppsplaying = 3; // playing
Wmppsscanforward = 4; // search forward
Wmppsscanreverse = 5; // search backward
Wmppsbuffering = 6; // Buffering
Wmppswaiting = 7; // waiting for the stream to start
Wmppsmediaended = 8; // The stream has ended.
Wmppstransitioning = 9; // prepare a new media file
Wmppsready = 10; // The playback is ready.
Wmppsreconnecting = 11; // try to reconnect the streaming media data
Wmppslast = 12; // The last time the status has not changed
Writing code in playstatechange can prevent playing non-default types such as rmvb (wmppsready is used ).
Enablecontextmenu: Boolean; enable/disable context menu
Fullscreen: Boolean; Whether to display in full screen

// Basic player control
Ctlcontrols. Play; play
Ctlcontrols. Pause; pause
Ctlcontrols. Stop; stop
Ctlcontrols. currentposition: Double; current progress
Ctlcontrols. currentpositionstring: string; current progress, string format. For example"
Ctlcontrols. fastforward; Fast Forward
Ctlcontrols. fastreverse; fast return
Ctlcontrols. Next; next
Ctlcontrols. Previous; previous line

[Settings] WMP. settings // basic player settings
Settings. Volume: integer; volume: 0-100
Settings. autostart: Boolean; Whether to play automatically
Settings. Mute: Boolean; mute
Settings. playcount: integer; number of playbacks
// Sequential playback
WMP. settings. setmode ("Shuffle", false)
// Random playback
WMP. settings. setmode ("Shuffle", true)
// Loop playback
WMP. settings. setmode ("loop", true)

[Currentmedia] WMP. currentmedia // current media attribute
Currentmedia. Duration: Double; total media Length
Currentmedia. durationstring: string; total media length, in string format. For example"
Currentmedia. getiteminfo (const string); get current media information "title" = media title, "author" = artist, "Copyright" = copyright information, "Description" = media content description, "duration" = duration (seconds), "filesize" = file size, "filetype" = file type, "sourceurl" = original address
Currentmedia. setiteminfo (const string); set media information by attribute name
Currentmedia. Name: string; same as currentmedia. getiteminfo ("title ")

[Currentplaylist] WMP. currentplaylist // attributes of the current playlist
Currentplaylist. Count: integer; number of media contained in the current playlist
Currentplaylist. item [integer]; gets or sets the media information of the specified project. Its subattributes are the same as that of WMP. currentmedia.
Axwindowsmediaplayer1.currentmedia. sourceurl; // obtain the path of the media file being played.
Axwindowsmediaplayer1.currentmedia. Name; // gets the name of the media file being played.
Axwindowsmediaplayer1.ctlcontrols. Play
Axwindowsmediaplayer1.ctlcontrols. Stop
Axwindowsmediaplayer1.ctlcontrols. Pause pause
Axwindowsmediaplayer1.ctlcontrols. playcount file playback times
Axwindowsmediaplayer1.ctlcontrols. autorewind loop playback (invalid)
Axwindowsmediaplayer1.ctlcontrols. Balance audio channel
Axwindowsmediaplayer1.ctlcontrols. Volume volume
Axwindowsmediaplayer1.ctlcontrols. Mute mute
Axwindowsmediaplayer1.enablecontextmenu whether to allow the shortcut menu to pop up when you right-click the control
Axwindowsmediaplayer1.ctlcontrols. animationatstart whether to play the animation before playback (invalid)
Axwindowsmediaplayer1.ctlcontrols. showcontrols: whether to display the control toolbar (invalid)
Axwindowsmediaplayer1.ctlcontrols. showaudiocontrols whether the sound control button is displayed (invalid)
Axwindowsmediaplayer1.ctlcontrols. showdisplay: whether the data file information is displayed (invalid)
Axwindowsmediaplayer1.ctlcontrols. showgotobar: whether the Goto column is displayed (invalid)
Axwindowsmediaplayer1.ctlcontrols. showpositioncontrols: whether to display the position adjustment button (invalid)
Axwindowsmediaplayer1.ctlcontrols. showstatusbar whether the status bar is displayed (invalid)
Axwindowsmediaplayer1.ctlcontrols. showtracker: whether the progress bar is displayed (invalid)
Axwindowsmediaplayer1.ctlcontrols. fastforward fast forward
Axwindowsmediaplayer1.ctlcontrols. fastreverse quick return
Axwindowsmediaplayer1.ctlcontrols. rate fast forward/Return Rate
Axwindowsmediaplayer1.allowchangedisplaysize: whether the playback image size can be set freely (invalid)
Axwindowsmediaplayer1.displaysize: sets the playback image size (invalid)
1-mpdefaultsize original size
2-mphalfsize: half of the original size
3-mpdoublesize: twice the original size
4-mpfullscreen full screen
5-mponesixteenthscreen screen size: 1/16
6-mponefourthscreen: 1/4 of screen size
7-mponehalfscreen: 1/2 of screen size
Axwindowsmediaplayer1.clicktoplay whether to enable media player in the playback window

After a video is played, you can read the width and height of the source video as follows, and then set it to the original size.
Private void resizeoriginal ()
{
Int intwidth = axwindowsmediaplayer1.currentmedia. imagesourcewidth;
Int intheight = axwindowsmediaplayer1.currentmedia. imagesourceheight;
Axwindowsmediaplayer1.width = intwidth + 2;
Axwindowsmediaplayer1.height = intheight + 2;
}

Open a media file and play it back:

Dim filepath as string
With me. openfiledialog1
. Title = "open a voice file"
. Checkpathexists = true
. Checkfileexists = true
. Multiselect = false
. Filter = "MP3 file (*. mp3) | *. MP3 | all files (*. *) | *.*"
If. showdialog = dialogresult. Cancel then
Exit sub
End if
Filepath =. filename
End
Me. Text = "PC repeater-file" & filepath
Axwindowsmediaplayer1.url = filepath
Try
Me. axwindowsmediaplayer1.ctlcontrols. Play ()
Catch ex as exception
Msgbox ("sorry, you cannot play audio files in this format", msgboxstyle. okonly, "PC repeater ")
Exit sub
End try
Note:
In axwindowsmediaplayer1.url, the URL indicates the file name to be played. The original name attribute is canceled.
Axwindowsmediaplayer1.ctlcontrols. Play () playback, other attributes such as pause and stop.
Axwindowsmediaplayer1.settings. Balance indicates the audio channel settings for media playback, 0 indicates the balance, and-1 and 1 indicates the Left and Right audio channels.
Axwindowsmediaplayer1.currentmedia. Duration indicates the duration of the file to be played. You can use it to obtain the file length.
Axwindowsmediaplayer1.ctlcontrols. currentposition indicates the current playback position of the file being played. This attribute can be used to set forward and backward for the media file. For example
Axwindowsmediaplayer1.ctlcontrols. currentposition + 1 indicates the first time unit forward.
Axwindowsmediaplayer1.settings. Rate: Specifies the playback rate. Generally, the unit of kbps is displayed after the value is multiplied by 16.
Note: In the above program, if you add:
Msgbox (axwindowsmediaplayer1.currentmedia. Duration. tostring)
The display result may be 0. Therefore, the playback duration of the file may not be obtained at this time, which is prone to errors. Therefore, you can add a timer control when using it:
Private sub timereffectick (byval sender as object, byval e as system. eventargs) handles timer1.tick
Endpoint = axwindowsmediaplayer1.currentmedia. Duration
If endpoint = 0 Then exit sub ', it may take some time to open the media file. It is waiting for the opening of the media file.
Msgbox (axwindowsmediaplayer1.currentmedia. Duration. tostring)
End sub
In this case, msgbox displays the playback length of the file.
2. ctlcontrols attributes
The ctlcontrols attribute is an important attribute of axwindowsmediaplayer, which has many common members.
(1) method play
Used to play multimedia files. The format is:
Form name. Control name. ctlcontrols. Play ()
For example, axwindowsmediaplayer1.ctlcontrols. Play () '. The default form name is me.
(2) method pause
Used to pause a video. The format is as follows:
Form name. Control name. ctlcontrols. Pause ()
For example, axwindowsmediaplayer1.ctlcontrols. Pause ()
(3) method stop
The format of a multimedia file used to stop playing is as follows:
Form name. Control name. ctlcontrols. Stop ()
For example, axwindowsmediaplayer1.ctlcontrols. Stop ()
(4) fastforward Method
This interface is used to fast forward a video. The format is as follows:
Form name. Control name. ctlcontrols. fastforward ()
For example, axwindowsmediaplayer1.ctlcontrols. Forward ()
(5) fastreverse
Form name. Control name. ctlcontrols. fastreverse ()
For example, axwindowsmediaplayer1.ctlcontrols. fastreverse ()
6. Attribute currentposition
Used to obtain the current playback progress of a multimedia file. The value is of the numerical type and the format is as follows:
Form name. Control name. ctlcontrols. currentposition
D1 = axwindowsmediaplayer1.ctlcontrols. currentposition
D1 is an integer variable.
7. Attribute duration
Used to obtain the total playback Time of the current multimedia file. The value is of the numerical type. The format is as follows:
Form name. Control name. currentmedia. Duration
For example, D2 = axwindowsmediaplayer1.currentmedia. Duration
D2 is an integer variable.
Sighs at WMP
Lament 1
This is the most comprehensive Chinese document about WMP. I checked again just now, found the more comprehensive API documentation on msdn, and finally found it. The above information is basically enough, but it is not enough to write a more sophisticated player. Refer to msdn. The position of wmp11 in msdn is as follows:
Win32 and COM development-griphics and multimedia-audio and vedio-Windows Media Player 11 SDK
I read the msdn and materials for one night. I found that WMP SDK documentation on msdn is not solid. No instance. None of the answers I am looking for are listed above. The only difference is that WPL is known. WMP has its own playlist file. However, I checked msdn and found that the SDK does not provide any method to manually Save the playlist. Therefore, the problem is that we have created an iwmpplaylist. However, this interface does not provide any save methods and results. Newplaylist (name, PATH) only allows you to open an existing list. Depressed. I checked the English documents. It is recommended for foreigners to use the streamwrite/streamread method to read and write WPL. I have also read it. In fact, WPL is an XML file. We can use the API in system. XML to read and write data. However, I have never been able to give up this leakage in the SDK.
Lament 2
The MMS/RTSP protocol is not supported since wmp8, and WMP. url = "MMS: // XXXX" is not supported. Click here for details, and the MMS protocol is still widely used. Depressed. Therefore, we cannot use WMP to watch network TVs.
WMP FAQs:
1. After WMP is upgraded, drag the Windows Media Player control to the form for an error (which has not been solved so far, it should be to modify the program file and use a text editor)
2. To play a file in an unofficial format such as rmvb/RM, double-click the file and click the play button, but the file cannot be played directly. The pop-up message box is displayed: Windows Media Player has encountered a problem in downloading the file. For help information, click "Web help ". The following two buttons are displayed: Disabled and web help. I have not found any official explanation for this problem. In my personal opinion, this is because WMP can only play the specified types of media by default. If not, WMP will try to download the corresponding decoder for playing. The download process fails, so the message box is displayed. WMP does not seem to determine whether the decoder already exists. But if you can play it again, why? It should be the decoder on the local machine for WMP's image search to play the media. How can we solve the problem of two mouse clicks? As follows:

Set the property WMP. settings. enableerrordialogs = false first. In fact, this property defaults to false. No need to set.

Private void wmp_playstatechange (Object sender, axwmplib. _ wmpocxevents_playstatechangeevent E)
{
// Play the next file after playing the video.
If (wmplib. wmpplaystate) E. newstate = wmplib. wmpplaystate. wmppsready) WMP. ctlcontrols. Play ();
}
Alas, I spent a lot of effort to find this solution.

3. How can I obtain information in a media file and modify some media information?

Method: axwindowsmediaplayer. newmedia (filename) creates an iwmpmedia instance. Use its setiteminfo. For information available for configuration, you can go to msdn to list all relevant information.

4. Is iwmpplaylist a chicken rib ????????
It is indeed a big chicken rib !!!!!!!!!!!

5. How many components of the player control can be hidden?

Media players include the following elements:
Video Display Panel: the video display panel;
Video border: Video border;
Closed captioning display panel; subtitle display panel;
Track bar; search bar;
Control bar with audio and position controls: control bar with sound and Position Control;
Go to bar: Go to the bar;
Display Panel: Display Panel;
Status Bar: status bar;

There is information on the Internet that controls provide methods to control whether they are displayed or not, but I did not find them in the SDK. The only thing that can be roughly controlled is the uimode attribute. The preceding values are available.

6. Where can I set the control's network settings, such as setting proxy, buffer count, and buffer time?

Axwindowsmediaplayer. Network. It is an iwmpnetwork instance.

7. Does the WMP control support subtitle-related settings?

Of course. Is axwindowsmediaplayer. closedcaption. It is an instance of iwmpclosedcaption.

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.