C# 在 WinForm 应用程序中播放 MP3 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15025626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing a MP3 file in a WinForm application
提问by user1139666
I am developing a WinForm
application. I want to play a MP3 file when the user clicks a button.
我正在开发一个WinForm
应用程序。我想在用户单击按钮时播放 MP3 文件。
The MP3 file is located in the file system of the computer where the application is executed.
I have Googled for a while and I have found information about the System.Media.SoundPlayer
class. But I have read that the SoundPlayer
class can only be used to play files in .wav format.
MP3 文件位于执行应用程序的计算机的文件系统中。
我在谷歌上搜索了一段时间,找到了有关该System.Media.SoundPlayer
课程的信息。但我读到SoundPlayer
该类只能用于播放 .wav 格式的文件。
What classes can be used to play files in .mp3 format ?
哪些类可用于播放 .mp3 格式的文件?
Any help will be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Max
The link below, gives a very good tutorial, about playing mp3 files from a windows form with c#:
下面的链接提供了一个非常好的教程,关于使用 c# 从 windows 窗体播放 mp3 文件:
http://www.daniweb.com/software-development/csharp/threads/292695/playing-mp3-in-c
http://www.daniweb.com/software-development/csharp/threads/292695/playing-mp3-in-c
This link will lead you to a topic, which contains a lot information about how to play an mp3 song, using Windows forms. It also contains a lot of other projects, trying to achieve the same thing:
此链接将引导您进入一个主题,其中包含有关如何使用 Windows 窗体播放 mp3 歌曲的大量信息。它还包含许多其他项目,试图实现相同的目标:
For example use this code for .mp3:
例如,将此代码用于 .mp3:
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "My MP3 file.mp3";
wplayer.Controls.Play();
Then only put the wplayer.Controls.Play(); in the Button_Click event.
然后只放 wplayer.Controls.Play(); 在 Button_Click 事件中。
For example use this code for .wav:
例如,将此代码用于 .wav:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "Sound.wav";
player.Play();
Put the player.Play(); in the Button_Click event, and it will work.
把 player.Play(); 在 Button_Click 事件中,它将起作用。
回答by VladL
1) The most simple way would be using WMPLib
1) 最简单的方法是使用WMPLib
WMPLib.WindowsMediaPlayer Player;
private void PlayFile(String url)
{
Player = new WMPLib.WindowsMediaPlayer();
Player.PlayStateChange += Player_PlayStateChange;
Player.URL = url;
Player.controls.play();
}
private void Player_PlayStateChange(int NewState)
{
if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
{
//Actions on stop
}
}
2) Alternatively you can use the open source library NAudio. It can play mp3 files using different methods and actually offers much more than just playing a file.
2) 或者,您可以使用开源库NAudio。它可以使用不同的方法播放 mp3 文件,实际上提供的不仅仅是播放文件。
This is as simple as
这很简单
using NAudio;
using NAudio.Wave;
IWavePlayer waveOutDevice = new WaveOut();
AudioFileReader audioFileReader = new AudioFileReader("Hadouken! - Ugly.mp3");
waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();
Don't forget to dispose after the stop
停车后别忘了处理
waveOutDevice.Stop();
audioFileReader.Dispose();
waveOutDevice.Dispose();
回答by Adiii
- first go to the properties of your project
- click on add references
add the library under COM object for window media player then type your code where you want
Source:WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); wplayer.URL = @"C:\Users\Adil M\Documents\Visual Studio 2012\adil.mp3"; wplayer.controls.play();
- 首先去你的项目的属性
- 单击添加引用
在窗口媒体播放器的 COM 对象下添加库,然后在您想要的位置键入代码
来源:WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer(); wplayer.URL = @"C:\Users\Adil M\Documents\Visual Studio 2012\adil.mp3"; wplayer.controls.play();
回答by krisliu
You can use the mciSendString
API to play an MP3 or a WAV file:
您可以使用mciSendString
API 播放 MP3 或 WAV 文件:
[DllImport("winmm.dll")]
public static extern uint mciSendString(
string lpstrCommand,
StringBuilder lpstrReturnString,
int uReturnLength,
IntPtr hWndCallback
);
mciSendString(@"close temp_alias", null, 0, IntPtr.Zero);
mciSendString(@"open ""music.mp3"" alias temp_alias", null, 0, IntPtr.Zero);
mciSendString("play temp_alias repeat", null, 0, IntPtr.Zero);
回答by Pedro Arruda
Refactoring:
重构:
new WindowsMediaPlayer() { URL = "MyMusic.mp3" }.controls.play();
回答by s?un????q?p
You can do it using old DirectShow functionality.
您可以使用旧的 DirectShow 功能来实现。
This answerteaches you how to create QuartzTypeLib.dll
:
这个答案教你如何创建QuartzTypeLib.dll
:
Run tlbimp tool (in your case path will be different):
Run
TlbImp.exe %windir%\system32\quartz.dll /out:QuartzTypeLib.dll
运行 tlbimp 工具(在您的情况下路径会有所不同):
跑
TlbImp.exe %windir%\system32\quartz.dll /out:QuartzTypeLib.dll
Alternatively, this projectcontains the library interop.QuartzTypeLib.dll
, which is basically the same thing as steps 1.and 2.The following steps teach how to use this library:
或者,这个项目包含 library interop.QuartzTypeLib.dll
,它与步骤1.和2基本相同。以下步骤教如何使用这个库:
Add generated QuartzTypeLib.dll as a COM-reference to your project (click right mouse button on the project name in "Solution Explorer", then select "Add" menu item and then "Reference")
In your Project, expand the "References", find the QuartzTypeLib reference. Right click it and select properties, and change "Embed Interop Types" to false. (Otherwise you won't be able to use the FilgraphManager class in your project (and probably a couple of other ones)).
In Project Settings, in the Build tab, I had to disable the Prefer 32-bit flag, Otherwise I would get this Exception:
System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040266
Use this class to play your favorite MP3file:
using QuartzTypeLib; public sealed class DirectShowPlayer { private FilgraphManager FilterGraph; public void Play(string path) { FilgraphManager = new FilgraphManager(); FilterGraph.RenderFile(path); FilterGraph.Run(); } public void Stop() { FilterGraph?.Stop(); } }
将生成的 QuartzTypeLib.dll 作为 COM 引用添加到您的项目中(在“解决方案资源管理器”中的项目名称上单击鼠标右键,然后选择“添加”菜单项,然后选择“引用”)
在您的项目中,展开“参考”,找到 QuartzTypeLib 参考。右键单击它并选择属性,然后将“嵌入互操作类型”更改为 false。(否则您将无法在您的项目中使用 FilgraphManager 类(可能还有其他几个))。
在项目设置的构建选项卡中,我必须禁用 Prefer 32-bit 标志,否则我会得到这个异常:
System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040266
使用这个类来播放你最喜欢的MP3文件:
using QuartzTypeLib; public sealed class DirectShowPlayer { private FilgraphManager FilterGraph; public void Play(string path) { FilgraphManager = new FilgraphManager(); FilterGraph.RenderFile(path); FilterGraph.Run(); } public void Stop() { FilterGraph?.Stop(); } }
PS:TlbImp.exe
can be found here:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
, or in
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools"
PS:TlbImp.exe
可以在这里找到:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin"
,或
"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools"