using AxWMPLib; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TestWindowsMediaPlayer { public class GPMediaPlayer : UserControl { string PlaylistName = "Topper"; public void SetPlaylist() { AxWMPLib.AxWindowsMediaPlayer player = null; WMPLib.IWMPPlaylistArray playlistArray; WMPLib.IWMPPlaylist playlist; if (player == null) { player = new AxWMPLib.AxWindowsMediaPlayer(); Controls.Add(player); player.Name = new Guid().ToString(); player.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left; player.Width = this.Width; player.Height = this.Height; player.Location = new System.Drawing.Point(0, 0); player.Visible = true; player.Ctlenabled = false; // player.PlayStateChange += new AxWMPLib._WMPOCXEvents_PlayStateChangeEventHandler(player_PlayStateChange); player.MediaError += new AxWMPLib._WMPOCXEvents_MediaErrorEventHandler(player_MediaError); // player.MediaChange += new AxWMPLib._WMPOCXEvents_MediaChangeEventHandler(player_MediaChange); } playlistArray = player.playlistCollection.getByName(PlaylistName); if (playlistArray != null && playlistArray.count > 0) { playlist = playlistArray.Item(0); player.currentPlaylist = playlist; player.uiMode = "none"; player.enableContextMenu = false; player.settings.setMode("loop", true); if (player.playState == WMPLib.WMPPlayState.wmppsStopped || player.playState == WMPLib.WMPPlayState.wmppsPaused || player.playState == WMPLib.WMPPlayState.wmppsReady) { player.Ctlcontrols.play(); } } } private void player_MediaError(object sender, _WMPOCXEvents_MediaErrorEvent e) { MessageBox.Show(e.ToString()); } } }
在项目文件里加COM引用:
<ItemGroup>
<COMReference Include="AxWMPLib">
<Guid>{6BF52A50-394A-11D3-B153-00C04F79FAA6}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>aximp</WrapperTool>
<Isolated>False</Isolated>
</COMReference>
<COMReference Include="WMPLib">
<Guid>{6BF52A50-394A-11D3-B153-00C04F79FAA6}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
</COMReference>
</ItemGroup>