首发于 无痛+英语
C# 怎么用几行代码录制和播放CD音质的声音 record/play CD quality audio in C# WTFPL

C# 怎么用几行代码录制和播放CD音质的声音 record/play CD quality audio in C# WTFPL

C# 怎么用几行代码做一个录制CD音质的录音机 How to make a CD quality audio recorder in C# with just a few lines ? WTFPL

代码亲测可用, 放心复制粘贴。做好的软件, 可以在这里下载:

wenxue.ca/wp-content/up



https://wenxue.ca/wp-content/uploads/2020/08/laomaiREC_Portable.zip

Windows XP x86 版本

wenxue.ca/wp-content/up

https://wenxue.ca/wp-content/uploads/2020/08/VoiceREC01__portable_x86.zip

如果您很不喜欢学编程,那就直接拿去用就是了。

**备注: 在 FEDORA 32 + WINE 的环境下也能使用, 尽管会有出错提示, 仍然可以录制到不错的声音。







//
// WTFPL
//  TAKE IT, BRO
using System;
using System.Collections;
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.Runtime.InteropServices;
using System.IO;
using System.Media;
//using System.Windows.Media;
namespace wFApp001
    public partial class Form1 : Form
        private System.Media.SoundPlayer myPlayer = new SoundPlayer();
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr HwndCallback);
        string ps_SoundLocation = Directory.GetCurrentDirectory();
        [DllImport("winmm.dll", EntryPoint = "PlaySound")]
        public static extern int PlaySound(string lpxzName, int hModul, int dwFlags);
        public Form1()
            InitializeComponent();
        private void Form1_Load(object sender, EventArgs e)
            btn_play.Visible = false;
            btn_stop.Visible = false;
        private void btn_rec_Click(object sender, EventArgs e)
            int lu_errcode;
            int lu_cch = 0;
            richTextBox1.Text = "";
            mciSendString("stop WaveDump" + "", "", 0, IntPtr.Zero);
            string lsb_ret = "";
            richTextBox1.Text += "\r\n" + lsb_ret + "\r\n";
            timer1.Enabled = true;
            btn_play.Visible = false;
            btn_stop.Visible = true;
            btn_stop.Enabled = true;
            string ls_mciRetV = "";
            Global.gdt_begin = DateTime.Now;
                mciSendString("open new type waveaudio alias WaveDump", ls_mciRetV, 0, IntPtr.Zero);
                // 切换不同的采样参数     
                mciSendString("set WaveDump time format ms bitspersample 16 channels 1 samplespersec 44100 bytespersec 88200 alignment 2", ls_mciRetV, 0, IntPtr.Zero); //Working Great
                lu_errcode = mciSendString("record WaveDump", ls_mciRetV, 0, IntPtr.Zero);
                this.Text = "Recording..." + ls_mciRetV;
                if (lu_errcode != 0)
                    richTextBox1.Text += "\r\n" + lsb_ret + "\r\n";
                    richTextBox1.Text += "\r\n" + lu_errcode.ToString() + " error(s). \r\n";
            catch (Exception ex)
                MessageBox.Show(ex.Message, "WTF, not my fault, Man...");
            btn_rec.BackColor = System.Drawing.Color.Pink; // "red";
            btn_stop.BackColor = SystemColors.Control;
        private void btn_stop_Click(object sender, EventArgs e)
            btn_rec.BackColor = SystemColors.Control;
            btn_rec.Enabled = false;
            int lu_errcode;
            int lu_cch = 0;
            string lsb_ret = "";
                lu_errcode = mciSendString("pause WaveDump", "", 0, IntPtr.Zero);
                this.Text = "Stopped.";
                richTextBox1.Text += "\r\n" + lsb_ret + "\r\n";
            catch (Exception ex)
                MessageBox.Show(ex.Message, "BLAHHH...");
            Global.gdt_end = DateTime.Now;
            btn_stop.BackColor = System.Drawing.Color.Gray; // "red";
            string timestamp = "_" + Global.gdt_begin.ToString().Replace(" ", "_").Replace(":", "t").Replace("/", "-") + "_till_" + Global.gdt_end.ToString().Replace(":", "t").Replace("/", "-").Replace(" ", "_") + "_.wav";
            ps_SoundLocation = Directory.GetCurrentDirectory() + "\\" + timestamp;
            mciSendString("save WaveDump " + ps_SoundLocation, "", 0, IntPtr.Zero);
            mciSendString("close WaveDump", "", 0, IntPtr.Zero);
            richTextBox1.Text += "\r\n" + ps_SoundLocation + " <-- Paste the PATH by CTRL-V.";
            this.Text = "Saved to " + ps_SoundLocation + " <-- The Path ";
            Global.gdt_begin = DateTime.Now;  // next episode
            btn_rec.Enabled = true;
            btn_stop.Enabled = false;
            btn_play.Visible = true;
            btn_play.Enabled = true;
            Clipboard.SetText(ps_SoundLocation);
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            timer1.Enabled = false;
        private Timer timer1 = new Timer();
        public static class Global
            public static string gs_clipurl = null;
            public static string gs_clippath = "c:\\"; //  C:\****\***\bin\Debug\
            ////DateTime.Now
            public static DateTime gdt_begin = DateTime.Now;
            public static DateTime gdt_end = DateTime.Now;
        private void btn_play_Click(object sender, EventArgs e)
            int lu_errcode = 1;
            string lsb_ret = "";
            int lu_cch = 0;
                mciSendString("stop" + "", "", 0, IntPtr.Zero);
            catch (Exception ex)
                MessageBox.Show(ex.Message, "Trying to play, but ....");
            if (ps_SoundLocation == Directory.GetCurrentDirectory() | ps_SoundLocation == "" | ps_SoundLocation.Length > 120)
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Wave|*.wav";
                if (open.ShowDialog() == DialogResult.OK) { ps_SoundLocation = open.FileName; }
                PlaySound(ps_SoundLocation, 0, 0x1);
                    btn_play.BackColor = System.Drawing.Color.Green;
                    lu_errcode = mciSendString("play " + ps_SoundLocation, "", 0, IntPtr.Zero);
                    this.Text = "Playing: " + ps_SoundLocation;
                    if (lu_errcode != 0)
                        richTextBox1.Text += "\r\n" + lsb_ret + " " + lu_cch.ToString() + " char(s).  " + "\r\n";
                        richTextBox1.Text += "\r\n \r\n " + lu_errcode.ToString() + " error(s)." + " " + lu_cch.ToString() + " char(s).  " + " \r\n";
                        richTextBox1.Text += "\r\n" + "Playing: " + ps_SoundLocation;
                catch (Exception ex)
                    MessageBox.Show(ex.Message, "Error !!! ....");
            btn_play.BackColor = SystemColors.Control;
            btn_stop.BackColor = SystemColors.Control;
            btn_rec.BackColor = SystemColors.Control;