基本信息
源码名称:录制声卡播放声音的C#源码
源码大小:7.73M
文件格式:.rar
开发语言:C#
更新时间:2017-12-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

一个录制声卡中播放声音的C#源码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESBasic;
using Oraycn.MCapture;
using Oraycn.MFile;

namespace Oraycn.RecordSoundCardDemo
{
    /*
    * 本demo采用的是 语音视频采集组件MCapture 和 语音视频录制组件MFile 的免费版本。若想获取MCapture、MFile其它版本,请联系 www.oraycn.com 。
    * 
    * 试用版:只能连续采集10分钟,连续录制5分钟!
    */
    public partial class Form1 : Form
    {       
        private ISoundcardCapturer soundcardCapturer;
        private AudioFileMaker audioFileMaker;        

        public Form1()
        {
            InitializeComponent();           

            Oraycn.MCapture.GlobalUtil.SetAuthorizedUser("FreeUser", "");
            Oraycn.MFile.GlobalUtil.SetAuthorizedUser("FreeUser", "");
        }        

        
        private volatile bool isRecording = false;
        //开始声卡采集、录制        
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //根据声卡采集器 【目前声卡采集仅支持vista以及以上系统】
                this.soundcardCapturer = CapturerFactory.CreateSoundcardCapturer();
                this.soundcardCapturer.AudioCaptured  = new ESBasic.CbGeneric<byte[]>(soundcardCapturer_AudioCaptured);
                this.soundcardCapturer.CaptureError  = new CbGeneric<Exception>(microphoneCapturer_CaptureError);
                //开始采集声卡
                this.soundcardCapturer.Start();

                this.audioCount = 0;
                this.audioFileMaker = new AudioFileMaker();              
                this.audioFileMaker.Initialize("test.mp3",AudioCodecType.MP3, this.soundcardCapturer.SampleRate, this.soundcardCapturer.ChannelCount);
                this.isRecording = true;

                this.button_startRecord.Enabled = false;
                this.button_stopRecord.Enabled = true;               
                this.label_recording.Visible = true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        //停止声卡采集、停止录制
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                CbGeneric cb = new CbGeneric(this.StopRecordAsyn);
                cb.BeginInvoke(null, null);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }  
        }

        private void StopRecordAsyn()
        {
            this.isRecording = false;
            this.soundcardCapturer.Stop();
            this.soundcardCapturer.Dispose(); //必须要释放声卡采集器!!!!!!!!
            this.audioFileMaker.Close(true);
            this.audioFileMaker.Dispose();
            this.audioFileMaker = null;
            this.AfterStopRecord();
        }

        private void AfterStopRecord()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new CbGeneric(this.AfterStopRecord));
            }
            else
            {
                this.button_startRecord.Enabled = true;
                this.button_stopRecord.Enabled = false;               
                this.label_recording.Visible = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show("录制完成!"   (this.audioCount * 0.05).ToString()   "秒。");
            }
        }

        private int audioCount = 0;
        void soundcardCapturer_AudioCaptured(byte[] audioData) //采集到的语音数据
        {
            if (this.isRecording)
            {
                this.audioFileMaker.AddAudioFrame(audioData);
                  this.audioCount;
            }
        }

        void microphoneCapturer_CaptureError(Exception obj)
        {

        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.isRecording)
            {
                MessageBox.Show("正在录制视频,请先停止!");
                e.Cancel = true;
                return;
            }           
        }        
    }
}