csharp中的记录功能问题 [英] Problem with a record function in csharp

查看:134
本文介绍了csharp中的记录功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个录音功能,可让我录制语音消息并将其保存在我的电脑中.然后,它使我们有可能听到录制的消息,问题是,当我安装外部声卡时,该功能会停止从默认声卡中录制声音,而从其他声卡中录制声音,所以您能给我一个想法吗?强制录音功能仅使用默认声卡.这是记录代码:

Hello

I have a record function which let me to record a vocal message and to save it in my pc. Then it gives us the possibility to hear the recorded message,the problem is that when I install an external sound card the function stops recording sound from my default sound card and it records it from the other audio card,so can you give me an idea to oblige the record function to use only the default sound card. This is the record code:

public partial class RecordingForm : Form
    {
        IWaveIn waveIn;
        WaveFileWriter writer;
        string outputFilename;
        public RecordingForm()
        {
            InitializeComponent();
            //LoadWasapiDevicesCombo();
        }
        private void LoadWasapiDevicesCombo()
        {
            MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
            MMDeviceCollection deviceCol = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
            Collection<MMDevice> devices = new Collection<MMDevice>();
            foreach (MMDevice device in deviceCol)
            {
                devices.Add(device);
            }
            this.comboDevices.DataSource = devices;
            this.comboDevices.DisplayMember = "FriendlyName";
        }
        private void buttonStartRecording_Click(object sender, EventArgs e)
        {
            if (waveIn == null)
            {
                if (outputFilename == null)
                {
                    buttonSelectOutputFile_Click(sender, e);
                }
                if (outputFilename == null)
                {
                    return;
                }
                waveIn = new WaveIn();
                waveIn.WaveFormat = new WaveFormat(8000, 1);
                writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
                waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
                waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
                waveIn.StartRecording();
                buttonStartRecording.Enabled = false;
            }
        }
        void waveIn_RecordingStopped(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler(waveIn_RecordingStopped), sender, e);
            }
            else
            {
                waveIn.Dispose();
                waveIn = null;
                writer.Close();
                writer = null;
                buttonStartRecording.Enabled = true;
                progressBar1.Value = 0;
                if (checkBoxAutoPlay.Checked)
                {
                    Process.Start(outputFilename);
                }
            }
        }
        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable), sender, e);
            }
            else
            {
                writer.WriteData(e.Buffer, 0, e.BytesRecorded);
                int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);
                if (secondsRecorded >= 30)
                {
                    StopRecording();
                }
                else
                {
                    progressBar1.Value = secondsRecorded;
                }
            }
        }
        void StopRecording()
        {
            Debug.WriteLine("StopRecording");
            waveIn.StopRecording();
        }
        private void buttonStopRecording_Click(object sender, EventArgs e)
        {
            if (waveIn != null)
            {
                StopRecording();
            }
        }
        private void buttonSelectOutputFile_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Select output file:";
            saveFileDialog.Filter = "WAV Files (*.wav)|*.wav";
            saveFileDialog.FileName = outputFilename;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                outputFilename = saveFileDialog.FileName;
            }
        }   
    }
}





在此先感谢您,并祝您美好.





Thank you in advance and good day

推荐答案

我认为这可能对您有所帮助:
C#MP3声音捕获/录制组件 [
I think this might help you :
C# MP3 Sound Capturing/Recording Component[^]


这篇关于csharp中的记录功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆