码迷,mamicode.com
首页 > Windows程序 > 详细

c#如何播放自己录制的声音 关于speak与SoundPlayer的基本用法

时间:2014-12-30 23:19:29      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

 

c#有自己定义的发声的类speak,但很多时候我们需要播放自己录制的声音文件等,此时用到了c#的发声器SoundPlayer,首先需要把自己的声音文件放在bin目录下的debug中,本例中声音文件夹名称为music,music下声音文件的名称为0到9,具体用法如下:

 

技术分享

 

具体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.IO;

namespace music
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       
        private void Form1_Load(object sender, EventArgs e)
        {
            

           
        }

        private SoundPlayer player;    //定义一个发声器
        string zhuzhu = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取和设置包括该应用程序的目录的名称。


        private void speak_Click(object sender, EventArgs e)
        {
            string sound = "";                //sound为文件的名字
            for (int i = 0; i < 10; i++)
            {
                sound = i.ToString();
                player = new System.Media.SoundPlayer();
                player.SoundLocation = zhuzhu + "music\\" + sound + ".wav";  //找到文件位置
                player.LoadAsync(); //从资源中加载.wav文件
                player.PlaySync();   //播放.wav文件
            }
        }
    }
}

 

 

如果需要用系统自带的声音发声时:

1.添加引用:using System.Speech.Synthesis;

2.SpeechSynthesizer speak = new SpeechSynthesizer();   //定义一个发音的类

3. speak.Speak(“您想要让他读的内容”);

 

c#如何播放自己录制的声音 关于speak与SoundPlayer的基本用法

标签:

原文地址:http://www.cnblogs.com/xfqj/p/4194703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!