我想使用wmp但是使用[DllImport()]。 我对这段代码并不熟悉,如何以这种方式播放声音?
Hi, I want to use wmp but with [DllImport()]. I am not familiar to this code, how can I play a sound in this way?
推荐答案DllImport不再是播放声音所必需的。 如果你真的需要使用DllImport来使用非托管声音API,请看看这篇文章:使用C#播放.wav文件 [ ^ ]。 (请注意,本文是用.Net 1.1编写的,现在在更新版本的框架中有更简单的方式播放声音) 如果你只是在玩.wav文件,不需要使用windows media player,你可以这样做: DllImport isn't really necessary for playing sounds anymore. If you really need to use DllImport to use the unmanaged sound APIs take a look at this article: Playing .wav files using C#[^]. (Note that this article is written with .Net 1.1 and there are much easier ways of playing sound now in the newer versions of the framework) If you are just playing .wav files and don't need to use windows media player, you could just do: System.Media.SoundPlayer player = new System.Media.SoundPlayer("SourceFile.wav"); player.Play();
如果您想播放其他媒体类型或者想要通过Windows媒体播放器播放声音,您首先要添加对C:\ Windows \ System32 \\的引用\\ wmp.dll然后使用此代码:
If you want to play other media types or actually want to play sounds through windows media player you first add a reference to C:\Windows\System32\wmp.dll and then use this code:
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayerClass(); wmp.URL = "SoundFile.wav"; wmp.controls.play();这支持媒体播放器支持的任何文件类型。 最后,你可以使用 XNA框架媒体命名空间 [ ^ ]。这支持大多数常见格式,但缺点是你需要引用XNA框架库:
This supports any file type that media player supports. Finally, you could use the XNA framework Media namespace[^]. This supports most of the common formats, but the downside is you'll need to reference the XNA framework libraries:
Uri songFile = new Uri("SoundFile.mp3"); Song yourSong = Song.FromUri("YourSong", songFile ); MediaPlayer.Play(yourSong);
更多推荐
使用Windows Media Player播放声音?
发布评论