您的位置:首页 > 编程语言 > C#

[转]高手看过来:winform里用C#怎么播放音频文件(例如:*.wav)?C#中怎样调用api函数和com组件?

2010-04-15 22:05 771 查看
http://topic.csdn.net/t/20051019/00/4335324.html

楼主beckham88(Icanfly)2005-10-19 00:45:10 在 .NET技术 / C# 提问
一共三个问题:
1,winform里用C#怎么播放音频文件(例如:*.wav)?
2,C#中怎样调用api函数?
3,C#中怎样调用com组件?

1 楼saucer(思归)回复于 2005-10-19 00:56:20 得分 40

1. http://www.codeguru.com/Csharp/Csharp/cs_graphics/sound/article.php/c6143/ http://blog.monstuff.com/archives/000070.html
2.
Platform Invoke Tutorial http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkplatforminvoketutorial.asp http://www.pinvoke.net/
3.
COM Interop Tutorials http://msdn.microsoft.com/library/en-us/csref/html/vcoriCOMInteropTutorial.asp?frame=true
3Top

2 楼jetxia(Thinking->Asking&Studying->Doing)回复于 2005-10-19 08:54:28 得分 20

1、可以调用MediaPlayer
2、比如
[DllImport("user32.dll", EntryPoint="SetWindowLong")]
public static extern int SetWindowLong (
int hwnd,
int nIndex,
int dwNewLong
);
3、把com组件在工程中添加引用

----------------

http://www.codeguru.com/Csharp/Csharp/cs_graphics/sound/article.php/c6143/

A Simple C# Class to Play .WAV Files in .NET



WavPlay

This is a simple program to test a class I wrote when I needed to play some .wav files from C#. It uses the simple Win32 command PlaySound—a throwback to earlier programming days, but it works. There are only two commands, play and stop, which are really all you need a lot of times. I wrapped it all in a simple class including the defines needed for the Play method. There is not much more to this other than it works on Win2K and WinXP and is simple to use.

One other interesting thing is I used the OpenFileDialog, a pre-configured dialog box, to get a .wav file path. It is very simple to use. Just drag it from the Toolbox to your dialog box. Watch out for the "Filter" property. The documentation says:

For each filtering option, the filter string contains a description of the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options are separated by the vertical bar.

The following is an example of a filter string: "Text files (*.txt)|*.txt|All files (*.*)|*.*"

You can add serveral filter patterns to a filter by separating the file types with smicolons. For example: "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"



Post a comment

Email Article

Print Article


Share Articles




Digg




del.icio.us




Newsvine




Facebook




Google




LinkedIn




MySpace




Reddit




Slashdot




StumbleUpon




Technorati




Twitter




Windows Live




YahooBuzz




FriendFeed


// initArticleMenu(document.getElementById("toolBoxShareMenu"));
// ]]>
It's fairly easy to understand, but watch out for spaces, and that the '|' is used in two ways: to denote the actual search string extension, and to separate different search types. The dialog has several interesting functions as well. Have fun.

Bill

public class WAVSounds{  [DllImport("WinMM.dll")]  public static extern bool  PlaySound(byte[]wfname, int fuSound);  //  flag values for SoundFlags argument on PlaySound  public int SND_SYNC       = 0x0000;      // play synchronously                                           // (default)  public int SND_ASYNC      = 0x0001;      // play asynchronously  public int SND_NODEFAULT  = 0x0002;      // silence (!default)                                           // if sound not found  public int SND_MEMORY     = 0x0004;      // pszSound points to                                           // a memory file  public int SND_LOOP       = 0x0008;      // loop the sound until                                           // next sndPlaySound  public int SND_NOSTOP     = 0x0010;      // don't stop any                                           // currently playing                                           // sound  public int SND_NOWAIT      = 0x00002000; // don't wait if the                                           // driver is busy  public int SND_ALIAS       = 0x00010000; // name is a Registry                                           // alias  public int SND_ALIAS_ID    = 0x00110000; // alias is a predefined                                           // ID  public int SND_FILENAME    = 0x00020000; // name is file name  public int SND_RESOURCE    = 0x00040004; // name is resource name                                           // or atom  public int SND_PURGE       = 0x0040;     // purge non-static                                           // events for task  public int SND_APPLICATION = 0x0080;     // look for application-                                           // specific association//-----------------------------------------------------------------public void Play(string wfname,int SoundFlags){  byte[] bname = new Byte[256];    //Max path length  bname = System.Text.Encoding.ASCII.GetBytes(wfname);  PlaySound(bname,SoundFlags);}//-----------------------------------------------------------------public void StopPlay(){  PlaySound(null,SND_PURGE);}//-----------------------------------------------------------------}   //End WAVSounds class//-----------------------------------------------------------------    public Form1()    {      // Required for Windows Form Designer support      //      InitializeComponent();      //      // TODO: Add any constructor code after InitializeComponent      // call      //        openFileDialog1.Title = "Select a Wave Sound File";        openFileDialog1.Filter = "Wav Files(*.wav)|*.wav|                                  All Files(*.*)|*.*";    }//-----------------------------------------------------------------private void FindFileButton_Click(object sender, System.EventArgs e){  openFileDialog1.FileName = m_wav_file.Text;  openFileDialog1.ShowDialog();  m_wav_file.Text = openFileDialog1.FileName;}//-----------------------------------------------------------------private void OnPlayButtonClick(object sender, System.EventArgs e){  WAVSounds ws = new WAVSounds();  ws.Play(m_wav_file.Text,ws.SND_ASYNC);}//-----------------------------------------------------------------private void StopButton_Click(object sender, System.EventArgs e){  WAVSounds ws = new WAVSounds();  ws.StopPlay();}//-----------------------------------------------------------------

Downloads

Download demo project - 106 Kb
Download source - 3 Kb

1

//文件下载: demo Project Source

http://blog.monstuff.com/archives/000070.html

Minimalistic C# media player

A while back (time flies), I wrote a little media player in C#. Its main characteristic is that it is window-less. All interactions are done using the media region and a circular context menu.

The purpose of this experiment was to play with the MediaPlayer component and interop, and to try a UI-less interface design. Although all features are not yet implemented, I believe this prototype gives a good idea of the possibilities of this approach.

Interface manipulation
The media rendering region can be manipulated by drag and dropping its nine areas. Dragging the center moves the "window" around, while clicking in the periphery resizes it. The cursor changes depending on its position to reflect this behavior.
Using the whole surface of the "window" to manipulate makes it more efficient, as it is hard to target the edges of regular windows to resize them.

The context menu is a set of eight buttons that appear in circle around the cursor. Their labels change depending on the state of the player. Open, Close, Quit, Play, Pause are implemented, while the Maximize, Seek, Volume and Options menus are not yet functional.
Using a context menu based interface gives quick mouse or stylus access to all the features while maximizing the available surface for the media rendering.

Future work
Beside implementing the missing features like Volume, Seek and Maximize, having the cursor auto-hide would be a nice addition.

The current version also has two problems:
- moving and resizing isn't always fluid and synchronized with the border of the window during the manipulation,
- the window's resize speed is sometimes slower than the cursor's, which leads the cursor outside of the window and the window to not receive the mouse events anymore.

The first problem occurs both when the media is paused and playing. Windows Media Player 9 has a similar behavior (on my Windows 2000 box).

The second problem is rather tricky: as soon as the cursor goes out of the window (because the window is resizing too slowly) the window stops receiving mouse events and therefore the resizing is interrupted. I believe using a system-wide hook to intercept mouse events could be a solution, although rather complex. Regular windows don't have this problem because their resizing is handled by the windows manager itself.

Downloads
Because the MediaPlayer renders using an overlay mechanism, I couldn't take a screenshot of the running app. So you'll have to try it for yourself.
Here is a zip of the executable, it also contains the two required interop dlls.
The source is available here.

Posted by Julien on June 28, 2003. Permalink

-----------

Recreated a MenuForm.resx (by copying Form1.resx ;-) and adapted the code to the new WMP10 object model.

There is one outstanding bug, which seems to come from WMP10 handling events differently when a video is running or not: when a video is running and you right click, the context menu appears and disappears instantly (instead of staying opened). Still trying to figure out a work-around.

A new zip is available at http://blog.monstuff.com/archives/images/MiniMediaPlayer.wmp10.src.zip

------- --------------

//文件下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: