mciSendString function (Windows) :
http://msdn.microsoft.com/en-us/library/windows/desktop/dd757161(v=vs.85).aspx
C#中DirectSound錄音的使用:
MP3基本介紹與LAME設定 :
http://cgclub.blog.hexun.com.tw/60766146_d.html
轉檔程式: WAV to MP3 converter in C#.NET (43883-04010-WAV-MP3-converter-C.NET.rar)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace WavToMP3
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
if (OpenWavFilesDialog.ShowDialog() == DialogResult.OK)
{
TotalFiles.Text = "Total Files = "+OpenWavFilesDialog.FileNames.Length.ToString();
}
}
private void SaveAsButton_Click(object sender, EventArgs e)
{
if (SaveMP3FolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
SaveFilesPath.Text=SaveMP3FolderBrowserDialog.SelectedPath.ToString();
}
}
private void ConvertButton_Click(object sender, EventArgs e)
{
if (OpenWavFilesDialog.FileNames.Length > 0 && SaveMP3FolderBrowserDialog.SelectedPath != "")
{
progressBar1.Visible = true;
BrowseButton.Enabled = false;
SaveAsButton.Enabled = false;
BitrateComboBox.Enabled = false;
SamplingrateComboBox.Enabled = false;
ConvertButton.Enabled = false;
try
{
progressBar1.Maximum = OpenWavFilesDialog.FileNames.Length;
foreach (string fn in OpenWavFilesDialog.FileNames)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = Application.StartupPath + @"\lame.exe";
psi.Arguments = "-b" + BitrateComboBox.SelectedItem.ToString() + " --resample " + SamplingrateComboBox.SelectedItem.ToString() + " -m j " +
"\"" + fn + "\"" + " " +
"\"" + SaveFilesPath.Text + "\\" + Path.GetFileNameWithoutExtension(fn)+".mp3" + "\"";
Process p = Process.Start(psi);
p.Close();
p.Dispose();
progressBar1.Value = progressBar1.Value + 1;
Thread.Sleep(1000);
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show(OpenWavFilesDialog.FileNames.Length + " File(s) Convertd Successfully", "Success");
progressBar1.Visible = false;
progressBar1.Value = 0;
TotalFiles.Clear();
BrowseButton.Enabled = true;
SaveFilesPath.Clear();
SaveAsButton.Enabled = true;
BitrateComboBox.Enabled = true;
SamplingrateComboBox.Enabled = true;
ConvertButton.Enabled = true;
OpenWavFilesDialog.Reset();
SaveMP3FolderBrowserDialog.Reset();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
}
}
private void MainForm_Load(object sender, EventArgs e)
{
BitrateComboBox.SelectedIndex = 8;
SamplingrateComboBox.SelectedIndex = 1;
}
private void BitrateComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
namespace WavToMP3
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TotalFiles = new System.Windows.Forms.TextBox();
this.SaveFilesPath = new System.Windows.Forms.TextBox();
this.BrowseButton = new System.Windows.Forms.Button();
this.SaveAsButton = new System.Windows.Forms.Button();
this.ConvertButton = new System.Windows.Forms.Button();
this.OpenWavFilesDialog = new System.Windows.Forms.OpenFileDialog();
this.SaveMP3FolderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
this.BitrateComboBox = new System.Windows.Forms.ComboBox();
this.SamplingrateComboBox = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// TotalFiles
//
this.TotalFiles.Enabled = false;
this.TotalFiles.Location = new System.Drawing.Point(12, 11);
this.TotalFiles.Name = "TotalFiles";
this.TotalFiles.ReadOnly = true;
this.TotalFiles.Size = new System.Drawing.Size(720, 22);
this.TotalFiles.TabIndex = 0;
//
// SaveFilesPath
//
this.SaveFilesPath.Enabled = false;
this.SaveFilesPath.Location = new System.Drawing.Point(12, 54);
this.SaveFilesPath.Name = "SaveFilesPath";
this.SaveFilesPath.ReadOnly = true;
this.SaveFilesPath.Size = new System.Drawing.Size(720, 22);
this.SaveFilesPath.TabIndex = 1;
//
// BrowseButton
//
this.BrowseButton.Location = new System.Drawing.Point(747, 9);
this.BrowseButton.Name = "BrowseButton";
this.BrowseButton.Size = new System.Drawing.Size(75, 21);
this.BrowseButton.TabIndex = 2;
this.BrowseButton.Text = "Browse";
this.BrowseButton.UseVisualStyleBackColor = true;
this.BrowseButton.Click += new System.EventHandler(this.BrowseButton_Click);
//
// SaveAsButton
//
this.SaveAsButton.Location = new System.Drawing.Point(747, 52);
this.SaveAsButton.Name = "SaveAsButton";
this.SaveAsButton.Size = new System.Drawing.Size(75, 21);
this.SaveAsButton.TabIndex = 3;
this.SaveAsButton.Text = "Save";
this.SaveAsButton.UseVisualStyleBackColor = true;
this.SaveAsButton.Click += new System.EventHandler(this.SaveAsButton_Click);
//
// ConvertButton
//
this.ConvertButton.Location = new System.Drawing.Point(747, 92);
this.ConvertButton.Name = "ConvertButton";
this.ConvertButton.Size = new System.Drawing.Size(75, 21);
this.ConvertButton.TabIndex = 4;
this.ConvertButton.Text = "Convert";
this.ConvertButton.UseVisualStyleBackColor = true;
this.ConvertButton.Click += new System.EventHandler(this.ConvertButton_Click);
//
// OpenWavFilesDialog
//
this.OpenWavFilesDialog.Filter = "WAV Files|*.wav";
this.OpenWavFilesDialog.Multiselect = true;
//
// BitrateComboBox
//
this.BitrateComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.BitrateComboBox.FormattingEnabled = true;
this.BitrateComboBox.Items.AddRange(new object[] {
"32",
"40",
"48",
"56",
"64",
"80",
"96",
"112",
"128",
"144",
"160"});
this.BitrateComboBox.Location = new System.Drawing.Point(168, 94);
this.BitrateComboBox.Name = "BitrateComboBox";
this.BitrateComboBox.Size = new System.Drawing.Size(121, 20);
this.BitrateComboBox.TabIndex = 5;
this.BitrateComboBox.SelectedIndexChanged += new System.EventHandler(this.BitrateComboBox_SelectedIndexChanged);
//
// SamplingrateComboBox
//
this.SamplingrateComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.SamplingrateComboBox.FormattingEnabled = true;
this.SamplingrateComboBox.Items.AddRange(new object[] {
"16",
"22.05",
"24"});
this.SamplingrateComboBox.Location = new System.Drawing.Point(480, 94);
this.SamplingrateComboBox.Name = "SamplingrateComboBox";
this.SamplingrateComboBox.Size = new System.Drawing.Size(121, 20);
this.SamplingrateComboBox.TabIndex = 6;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(92, 97);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(71, 12);
this.label1.TabIndex = 7;
this.label1.Text = "Bitrate (Kbps)";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(369, 97);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(105, 12);
this.label2.TabIndex = 8;
this.label2.Text = "Sampling Rate (KHz)";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(12, 31);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(720, 21);
this.progressBar1.TabIndex = 9;
this.progressBar1.Visible = false;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(834, 122);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.SamplingrateComboBox);
this.Controls.Add(this.BitrateComboBox);
this.Controls.Add(this.ConvertButton);
this.Controls.Add(this.SaveAsButton);
this.Controls.Add(this.BrowseButton);
this.Controls.Add(this.SaveFilesPath);
this.Controls.Add(this.TotalFiles);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Wav To MP3";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox TotalFiles;
private System.Windows.Forms.TextBox SaveFilesPath;
private System.Windows.Forms.Button BrowseButton;
private System.Windows.Forms.Button SaveAsButton;
private System.Windows.Forms.Button ConvertButton;
private System.Windows.Forms.OpenFileDialog OpenWavFilesDialog;
private System.Windows.Forms.FolderBrowserDialog SaveMP3FolderBrowserDialog;
private System.Windows.Forms.ComboBox BitrateComboBox;
private System.Windows.Forms.ComboBox SamplingrateComboBox;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ProgressBar progressBar1;
}
}
錄音程式: WinFormsRecord.zip
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 Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;
namespace WinFormsRecord
{
public partial class Form1 : Form
{
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
//[DllImport("winmm.dll", EntryPoint = "mciSendStringW", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
public Form1()
{
InitializeComponent();
}
private void btnRecord_Click(object sender, EventArgs e)
{
// record from microphone
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
}
private void btnSaveStop_Click(object sender, EventArgs e)
{
// stop and save
mciSendString("save recsound D:\\record.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
Computer c = new Computer();c.Audio.Stop();
}
private void btnRead_Click(object sender, EventArgs e)
{
Computer computer = new Computer();
computer.Audio.Play("D:\\record.wav", AudioPlayMode.Background);
}
}
}
MainForm.Designer.cs
namespace WinFormsRecord
{
partial class Form1
{
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
private void InitializeComponent()
{
this.btnRecord = new System.Windows.Forms.Button();
this.btnSaveStop = new System.Windows.Forms.Button();
this.btnRead = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnRecord
//
this.btnRecord.Location = new System.Drawing.Point(13, 13);
this.btnRecord.Name = "btnRecord";
this.btnRecord.Size = new System.Drawing.Size(75, 23);
this.btnRecord.TabIndex = 0;
this.btnRecord.Text = "Record";
this.btnRecord.UseVisualStyleBackColor = true;
this.btnRecord.Click += new System.EventHandler(this.btnRecord_Click);
//
// btnSaveStop
//
this.btnSaveStop.Location = new System.Drawing.Point(95, 13);
this.btnSaveStop.Name = "btnSaveStop";
this.btnSaveStop.Size = new System.Drawing.Size(75, 23);
this.btnSaveStop.TabIndex = 1;
this.btnSaveStop.Text = "SaveStop";
this.btnSaveStop.UseVisualStyleBackColor = true;
this.btnSaveStop.Click += new System.EventHandler(this.btnSaveStop_Click);
//
// btnRead
//
this.btnRead.Location = new System.Drawing.Point(177, 13);
this.btnRead.Name = "btnRead";
this.btnRead.Size = new System.Drawing.Size(75, 23);
this.btnRead.TabIndex = 2;
this.btnRead.Text = "Read";
this.btnRead.UseVisualStyleBackColor = true;
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(268, 58);
this.Controls.Add(this.btnRead);
this.Controls.Add(this.btnSaveStop);
this.Controls.Add(this.btnRecord);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnRecord;
private System.Windows.Forms.Button btnSaveStop;
private System.Windows.Forms.Button btnRead;
}
}
沒有留言:
張貼留言