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.Diagnostics; using System.IO; using System.Threading; namespace Win { public partial class Form1 : Form { public static string url=""; public Form1() { InitializeComponent(); } static FileSystemWatcher watcher = new FileSystemWatcher(); /// <summary> /// 初始化監聽 /// </summary> /// <param name="StrWarcherPath">需要監聽的目錄</param> /// <param name="FilterType">需要監聽的文件類型(篩選器字符串)</param> /// <param name="IsEnableRaising">是否啟用監聽</param> /// <param name="IsInclude">是否監聽子目錄</param> private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude) { //初始化監聽 watcher.BeginInit(); //設置監聽文件類型 watcher.Filter = FilterType; //設置是否監聽子目錄 watcher.IncludeSubdirectories = IsInclude; //設置是否啟用監聽? watcher.EnableRaisingEvents = IsEnableRaising; //設置需要監聽的更改類型(如:文件或者文件夾的屬性,文件或者文件夾的創建時間;NotifyFilters枚舉的內容) watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; //設置監聽的路徑 watcher.Path = StrWarcherPath; //注冊創建文件或目錄時的監聽事件 watcher.Created += new FileSystemEventHandler(watch_created); //注冊當指定目錄的文件或者目錄發生改變的時候的監聽事件 watcher.Changed += new FileSystemEventHandler(watch_changed); //注冊當刪除目錄的文件或者目錄的時候的監聽事件 watcher.Deleted += new FileSystemEventHandler(watch_deleted); //當指定目錄的文件或者目錄發生重命名的時候的監聽事件 watcher.Renamed += new RenamedEventHandler(watch_renamed); //結束初始化 watcher.EndInit(); } /// <summary> /// 創建文件或者目錄時的監聽事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void watch_created(object sender, FileSystemEventArgs e) { //事件內容 string fileName = e.FullPath; string targetFileName = ""; WavConvertAmr.ConvertToMP3 toamr = new WavConvertAmr.ConvertToMP3(); try { if (!System.IO.Path.GetExtension(fileName).Equals(".mp3", StringComparison.CurrentCultureIgnoreCase)) { targetFileName = e.FullPath.Substring(0, e.FullPath.LastIndexOf(".")) + ".mp3"; toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName); } //轉換采樣率 targetFileName = e.FullPath.Substring(0, e.FullPath.LastIndexOf(".")) + ".wav"; toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 當指定目錄的文件或者目錄發生改變的時候的監聽事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void watch_changed(object sender, FileSystemEventArgs e) { //事件內容 //MessageBox.Show("發生改變"); } /// <summary> /// 當刪除目錄的文件或者目錄的時候的監聽事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void watch_deleted(object sender, FileSystemEventArgs e) { //事件內容 } /// <summary> /// 當指定目錄的文件或者目錄發生重命名的時候的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void watch_renamed(object sender, RenamedEventArgs e) { //事件內容 } /// <summary> /// 啟動或者停止監聽 /// </summary> /// <param name="IsEnableRaising">True:啟用監聽,False:關閉監聽</param> private void WatchStartOrSopt(bool IsEnableRaising) { watcher.EnableRaisingEvents = IsEnableRaising; } private void button2_Click(object sender, EventArgs e) { Record re = new Record(); re.Show(); } private void button1_Click_1(object sender, EventArgs e) { //url = System.AppDomain.CurrentDomain.BaseDirectory.Replace("WavConvertAmr\\Win\\bin\\Debug\\", "") + textBox1.Text; try { url = System.AppDomain.CurrentDomain.BaseDirectory.Replace("Debug\\", "") + textBox1.Text; WatcherStrat(url, "*", true, false); } catch (Exception ex) { throw new Exception(ex.Message); } this.panel1.Visible = true; MessageBox.Show("啟動成功!"); } private void button2_Click_1(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { string fileName = open.FileName; string targetFileName = open.FileName.Substring(0, open.FileName.LastIndexOf(".")) + ".wav"; WavConvertAmr.ConvertToMP3 toamr = new WavConvertAmr.ConvertToMP3(); toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName); MessageBox.Show("轉換成功"); } } private void panel1_Paint(object sender, PaintEventArgs e) { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace WavConvertAmr { public class ConvertToMP3 { /// <summary> /// 將Wav音頻轉成Amr手機音頻 /// </summary> /// <param name="applicationPath">ffmeg.exe文件路徑</param> /// <param name="fileName">WAV文件的路徑(帶文件名)</param> /// <param name="targetFilName">生成目前amr文件路徑(帶文件名)</param> public void ConvertToAmr(string applicationPath, string fileName, string targetFilName) { //string c = applicationPath + @"\ffmpeg.exe -y -i " + fileName + " -ar 8000 -ab 128 -ac 1 " + targetFilName; string c = "\"" + applicationPath + @"\ffmpeg.exe" + "\"" + " -i " + "\"" + fileName + "\"" + " -ar 8000 -y " + "\"" + targetFilName + "\""; Cmd(c); } /// <summary> /// 執行Cmd命令 /// </summary> private void Cmd(string c) { try { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); process.StandardInput.WriteLine(c); process.StandardInput.AutoFlush = true; process.StandardInput.WriteLine("exit"); StreamReader reader = process.StandardOutput;//截取輸出流 //process.WaitForExit(); } catch { } } } }