c# 播放声音的四种方法

news/2024/10/22 16:45:04/

第一种是利用DirectX

1.安装了DirectX SDK(有9个DLL文件)。这里我们只用到MicroSoft.DirectX.dll 和 Microsoft.Directx.DirectSound.dll
2.引入DirectX 的DLL文件的名字空间:

using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

3.建立设备

Device dv=new Device();

4.设置CooperativeLevel。因为windows是多任务的系统,设备不是独占的

SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);

5.开辟缓冲区

SecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);

6.接下来就可以播放啦。第一个参数表示优先级别,0是最低的。第2个参数是播放方式,这里是循环播放。

buf.Play(0,BufferPlayFlags.Looping);

第二种是利用Microsoft speech object Library

/// <summary/// 播放声音文件/// </summary>/// <param name="FileName">文件全名</param>public void PlaySound(string FileName){//要加载COM组件:Microsoft speech object Libraryif (!System.IO.File.Exists(FileName)){return;}SpeechLib.SpVoiceClass pp = new SpeechLib.SpVoiceClass();SpeechLib.SpFileStreamClass spFs = new SpeechLib.SpFileStreamClass();spFs.Open(FileName, SpeechLib.SpeechStreamFileMode.SSFMOpenForRead, true);SpeechLib.ISpeechBaseStream Istream = spFs as SpeechLib.ISpeechBaseStream;pp.SpeakStream(Istream, SpeechLib.SpeechVoiceSpeakFlags.SVSFIsFilename);spFs.Close();}

第三种:引用SoundPlayer

System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath+@"/pm3.wav");sndPlayer.PlayLooping();

第4种:利用Windows Media Player

新建一个C#的Windows Form工程(Windows应用程序),并且定义两个菜单按钮(menuItem1,menuItem2)。
选择菜单中的“工具”中的“自定义工具箱(添加/移除工具箱项)”,在自定义工具箱的窗口中,点击展开“COM 组件”项,选中“Window Media Player”选项。确定后在“工具箱”中便会出现“Windows Media Player”这一项,然后再将其拖至Form上,调整大小,系统在“引用”中自动加入了对此dll的引用,AxMediaPlayer就是我们使用的Namespace与class。

在属性栏中设置好此控件的一些属性,为了方便,这里我把AutoStart设置成为true(其实默认是true),只要FileName被设置(打开了文件),则文件将会自动播放。完整代码如下:

private void menuItem1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog();
ofDialog.AddExtension = true;
ofDialog.CheckFileExists = true;
ofDialog.CheckPathExists = true;//the next sentence must be in single line
ofDialog.Filter = "VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi|WAV文件(*.wav)|*.wav|MP3文件(*.mp3)|*.mp3|所有文件 (*.*)|*.*";ofDialog.DefaultExt = "*.mp3";
if(ofDialog.ShowDialog() == DialogResult.OK)
{
// 2003一下版本 方法 this.axMediaPlayer1.FileName = ofDialog.FileName;
this.axMediaPlayer1.URL= ofDialog.FileName;//2005用法}
}

这里使用的是微软的播放器,大家也可以试试Winamp的控件,如果c#教程你只需要播放声音而不需要显示,你只要把AxMediaPlayer的Visible属性设置为false就可以了。


http://www.ppmy.cn/news/567212.html

相关文章

Fmod学习3将音频文件变音播放

本文在FMOD学习2基础 简介&#xff1a;本次变音大量借鉴 xxx学院的教学 。我这里做下笔记 参考官方demo&#xff1a;effects.cpp dsp_effect_per_speaker.cpp 这里的代码是基于FMOD1的代码 MainActivity package com.example.fmy.selfplaymusic;import android.content.Co…

如何使Winamp看起来像iTunes

Winamp is a terrific, customizable media player that keeps getting better with each new release. If you are tired of the default skins included with it, then join us as we show you how to make Winamp look like iTunes. Winamp是一个了不起的&#xff0c;可自定…

winamp 5.55下载_免费下载:Winamp 5.8 Beta已正式发布,现在对Llama友好

winamp 5.55下载 The tech world got really excited the other day with the news that Winamp was supposedly returning with an all-new version that would solve all of our music-listening problems. And now the official Winamp 5.8 is available for download. Grea…

winamp 珍藏_Winamp 6可能不是汽配并且可能在2019年到货(可能)

winamp 珍藏 Llama Llama, red pajama, reads a story with his mama… and according to this story in Techcrunch today, Winamp might be coming back next year as version 6—but just like this paragraph, it doesn’t make any sense. Llama红色睡衣的Llama和他的妈妈…

Winamp v5.6.6.3516

说是最后一版&#xff0c;如今还有多少人记得它&#xff1f;下载&#xff1a;http://download.nullsoft.com/winamp/client/winamp5666_full_all.exe【特别信息】Name: ThumperDC Key: 1C52M-TVS6H-O3JBZ-QD49EName: ThumperTM Key: 6U74J-TSCCX-172KP-W4NFDName: ThumperDC.CO…

winamp 珍藏_Winamp发生了什么,您现在可以使用吗?

winamp 珍藏 () Twenty years ago Winamp was the future. Now it’s a distant memory. What happened? 二十年前&#xff0c;Winamp是未来。 现在是遥远的记忆。 发生了什么&#xff1f; Winamp (Windows Advanced Multimedia Products) came out on April 21, 1997—back …

一代传奇!经典播放器 Winamp 的开源实现

【导语】&#xff1a;Winamp 是一个超级经典的音频播放器&#xff0c;诞生于 1997 年。它随着 MP3 共享文件的繁荣而迅速走红&#xff0c;如今已经名存实亡了。 用过 Winamp 这个软件的人中&#xff0c;80 后应该居多。 Winamp 有很多插件&#xff0c;比如&#xff1a;Winamp…

HTML5实现Winamp2.9音频播放器插件

Winamp2-js是一款使用 html5和javascript来实现Winamp 2.9 音频播放器的插件。该Winamp音频播放器插件有支持拖拽文件&#xff0c;自定义皮肤&#xff0c;支持可视化模式等特点。特点还有&#xff1a; 实际的Winamp皮肤文件存储在本地计算机上&#xff0c;你可以任意调用自己…