使用Windows Media Player播放声音?

编程入门 行业动态 更新时间:2024-10-10 21:23:43
本文介绍了使用Windows Media Player播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用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播放声音?

本文发布于:2023-11-05 18:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561522.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   Windows   Media   Player

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!