请选中您要保存的内容,粘贴到此文本框
此项目需求是针对.wav格式音频进行操作,转换成相应的.mp3格式的音频文件,对音频进行切割,最后以需求的形式输出,此篇会回顾运用到的一些知识点。
1.MDI子窗口的建立:
首先一个窗体能够创建多个MDI窗体,应当将IsMDIContainer属性设为true;以下为效果图:
![](http://image66.360doc.com/DownloadImg/2013/10/2714/36271780_1.jpg)
控制窗体切换的是一个DotNetBar.TabStrip控件,style属性为Office2007Document,TabLayOutType:FixedWithNavigationBox
创建窗体的代码如下:
09 |
public static Form MainForm { get ; set ; } |
15 |
<TYPEPARAM name= "T" > 窗口类型 |
17 |
public static void CreateChildWindow |
18 |
<T> () where T : Form, new () |
19 |
// where 子句还可以包括构造函数约束。 可以使用 new 运算符创建类型参数的实例;但类型参数为此必须受构造函数约束 |
20 |
// new() 的约束。 new() 约束可以让编译器知道:提供的任何类型参数都必须具有可访问的无参数(或默认)构造函数。 |
24 |
var childForms = MainForm.MdiChildren; |
26 |
foreach (Form f in childForms) |
40 |
form.Icon = System.Drawing.Icon.FromHandle(Properties.Resources.MainIcon.GetHicon()); |
42 |
form.MdiParent = MainForm; |
44 |
form.FormBorderStyle = FormBorderStyle.FixedToolWindow; |
47 |
form.WindowState = FormWindowState.Maximized; |
前台点击按钮调用代码:CreateMDIWindow.CreateChildWindow (); <>里为窗体的名称。
2.序列化与反序列化:
当一个系统你有默认的工作目录,默认的文件保存路径,且这些数据时唯一的,你希望每次打开软件都会显示这些数据,也可以更新这些数据,可以使用序列化与反序列化。
![](http://image66.360doc.com/DownloadImg/2013/10/2714/36271780_2.jpg)
我们以项目存储根目录和选择项目为例:
代码如下:
02 |
public class UserSetting |
07 |
private string FilePath{ get { return Path.Combine(Environment.CurrentDirectory, "User.data" ); } } |
12 |
public string AudioResourceFolder { get ; set ; } |
17 |
public string Solution { get ; set ; } |
24 |
if (!File.Exists(FilePath)) |
26 |
FileStream fs = File.Create(FilePath); |
27 |
fs.Close(); //不关闭文件流,首次创建该文件后不能被使用买现成会被占用 |
34 |
public UserSetting ReadUserSetting() |
36 |
using (FileStream fs = new FileStream(FilePath, FileMode.Open,FileAccess.Read)) |
41 |
SoapFormatter sf = new SoapFormatter(); |
42 |
ob = sf.Deserialize(fs); |
44 |
return ob as UserSetting; |
51 |
public void SaveUserSetting( object obj) |
53 |
using (FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Write)) |
55 |
SoapFormatter sf = new SoapFormatter(); |
3.Datagridview动态生成:
![](http://image66.360doc.com/DownloadImg/2013/10/2714/36271780_3.jpg)
根据设置的楼层生成相应楼层带button按钮的datagridview,并且每层按钮为每层选定选择音乐,代码如下:
04 |
private void BindData( int elevatorLow, int number) |
08 |
DataTable list = new DataTable(); |
10 |
list.Columns.Add( new DataColumn( "name" , typeof ( string ))); |
11 |
list.Columns.Add( new DataColumn( "musicPath" , typeof ( string ))); |
12 |
for ( int i =0; i < number; i++) |
17 |
list.Rows.Add(list.NewRow()); |
18 |
list.Rows[i][0] = elevatorLow; |
23 |
dataGridViewX1.DataSource = list; |
26 |
{ MessageBox.Show(ex.ToString()); } |
选择音乐按钮事件:
01 |
private void dataGridViewX1_CellContentClick( object sender, DataGridViewCellEventArgs e) |
08 |
DataGridViewColumn column = dataGridViewX1.Columns[e.ColumnIndex]; |
09 |
if (column is DataGridViewButtonColumn) |
11 |
OpenFileDialog openMusic = new OpenFileDialog(); |
12 |
openMusic.AddExtension = true ; |
13 |
openMusic.Multiselect = true ; |
14 |
openMusic.Filter = "MP3文件(*.mp3)|*mp3" ; |
15 |
if (openMusic.ShowDialog() == DialogResult.OK) |
17 |
dataGridViewX1.Rows[e.RowIndex].Cells[2].Value = Path.GetFileName(openMusic.FileName); |
23 |
{ MessageBox.Show(ex.ToString()); } |
4.获得音乐文件属性:
使用Shellclass获得文件属性可以参考 点击打开链接
![](http://image66.360doc.com/DownloadImg/2013/10/2714/36271780_4.jpg)
代码如下:
04 |
/// <PARAM name="filePath" />文件的完整路径 |
05 |
public static string [] GetMP3Time( string filePath) |
07 |
string dirName = Path.GetDirectoryName(filePath); |
08 |
string SongName = Path.GetFileName(filePath); //获得歌曲名称 |
09 |
ShellClass sh = new ShellClass(); |
10 |
Folder dir = sh.NameSpace(dirName); |
11 |
FolderItem item = dir.ParseName(SongName); |
12 |
string SongTime = dir.GetDetailsOf(item, 27); //27为获得歌曲持续时间 ,28为获得音乐速率,1为获得音乐文件大小 |
13 |
string [] time = Regex.Split(SongTime, ":" ); |
5.音频操作:
音频的操作用的fmpeg.exe ,下载地址
fmpeg放在bin目录下,代码如下:
04 |
/// <PARAM name="exe" />ffmpeg程序 |
05 |
/// <PARAM name="arg" />执行参数 |
06 |
public static void ExcuteProcess( string exe, string arg) |
08 |
using (var p = new Process()) |
10 |
p.StartInfo.FileName = exe; |
11 |
p.StartInfo.Arguments = arg; |
12 |
p.StartInfo.UseShellExecute = false ; //输出信息重定向 |
13 |
p.StartInfo.CreateNoWindow = true ; |
14 |
p.StartInfo.RedirectStandardError = true ; |
15 |
p.StartInfo.RedirectStandardOutput = true ; |
17 |
p.BeginOutputReadLine(); |
18 |
p.BeginErrorReadLine(); |
19 |
p.WaitForExit(); //等待进程结束 |
音频转换的代码如下:
01 |
private void btnConvert_Click( object sender, EventArgs e) |
04 |
if (txtMp3Music.Text != "" ) |
06 |
string fromMusic = Statics.Setting.AudioResourceFolder + "\\" + Statics.Setting.Solution+ "\\" + cobFolders.Text + "\\" + txtMusic.Text; //转换音乐路径 |
07 |
string toMusic = Statics.Setting.AudioResourceFolder + "\\" + Statics.Setting.Solution+ "\\" + cobFolders.Text + "\\" + txtMp3Music.Text; //转换后音乐路径 |
08 |
int bitrate = Convert.ToInt32(cobBitRate.Text) * 1000; //恒定码率 |
09 |
string Hz = cobHz.Text; //采样频率 |
13 |
MP3Convertion.ExcuteProcess( "ffmpeg.exe" , "-y -ab " + bitrate + " -ar " + Hz + " -i \"" + fromMusic + "\" \"" + toMusic + "\"" ); |
14 |
if (cbRetain.Checked == false ) |
16 |
File.Delete(fromMusic); |
21 |
foreach (ListViewItem lt in listMusics.Items) |
23 |
if (lt.Text == txtMusic.Text) |
25 |
listMusics.Items.Remove(lt); |
31 |
MessageBox.Show( "转换完成" ); |
33 |
txtMp3Music.Text = "" ; |
36 |
{ MessageBox.Show(ex.ToString()); } |
40 |
MessageBox.Show( "请选择你要转换的音乐" ); |
音频切割的代码如下:
01 |
private void btnCut_Click( object sender, EventArgs e) |
03 |
SaveFileDialog saveMusic = new SaveFileDialog(); |
04 |
saveMusic.Title = "选择音乐文件存放的位置" ; |
05 |
saveMusic.DefaultExt = ".mp3" ; |
06 |
saveMusic.InitialDirectory = Statics.Setting.AudioResourceFolder + "\\" + Statics.Setting.Solution+ "\\" + cobFolders.Text; |
07 |
string fromPath = Statics.Setting.AudioResourceFolder + "\\" + Statics.Setting.Solution + "\\" + cobFolders.Text + "\\" + txtMusic.Text; //要切割音乐的物理路径 |
08 |
string startTime = string .Format( "0:{0}:{1}" , txtBeginM.Text, txtBeginS.Text).Trim(); //歌曲起始时间 |
09 |
int duration = (Convert.ToInt32( this .txtEndM.Text) * 60 + Convert.ToInt32( this .txtEndS.Text)) - (Convert.ToInt32( this .txtBeginM.Text) * 60 + Convert.ToInt32( this .txtBeginS.Text)); |
10 |
string endTime = string .Format( "0:{0}:{1}" , duration / 60, duration % 60); //endTime是持续的时间,不是歌曲结束的时间 |
11 |
if (saveMusic.ShowDialog() == DialogResult.OK) |
13 |
string savePath = saveMusic.FileName; //切割后音乐保存的物理路径 |
16 |
MP3Convertion.ExcuteProcess( "ffmpeg.exe" , "-y -i \"" + fromPath + "\" -ss " + startTime + " -t " + endTime + " -acodec copy \"" + savePath+ "\"" ); //-acodec copy表示歌曲的码率和采样频率均与前者相同 |
17 |
MessageBox.Show( "已切割完成" ); |
21 |
MessageBox.Show(ex.ToString()); |
切割音频操作系统的知识点就总结道这了,就是fmpeg的应用。
- ffmpeg命令操作音频格式转换
1.转MP3为wav ffmpeg -i input.mp3 -acodec pcm_s16le -ac 2 -ar 44100 output.wav 2.转m4a为wav ffmpeg -i inp ...
- 使用ffmpeg 操作音频文件前后部分静音移除.
指令特别简单, 但是却琢磨了一下午. 总结看文档时要细心, 主要ffmpeg的版本要 8.2.1 以上 ffmpeg -i in.mp3 -af silenceremove=start_periods ...
- ASOC 音频子系统框架
基于: Mini2440 开发板, Linux 3.4.2 内核 ASOC 简介: ASoC - ALSA System on Chip,是建立在标准ALSA驱动层上,为了更好地支持嵌入式处理器和移动 ...
- 音频 API 一览
iOS 和 OS X 平台都有一系列操作音频的 API,其中涵盖了从低到高的全部层级.随着时间的推移.平台的增长以及改变,不同 API 的数量可以说有着非常巨大的变化.本文对当前可以使用的 API 以 ...
- 音频单元组件服务参考(Audio Unit Component Services Reference)
目录 了解Audio Unit体系结构 文档结构预览 结构单元介绍 本文主要介绍AudioUnit的组成 本文由自己理解而成,如有错误,请欢迎网友们指出校正. 了解Audio Unit体系结构 开始前 ...
- iOS 音频开发之CoreAudio
转自:http://www.cnblogs.com/javawebsoa/archive/2013/05/20/3089511.html 接 触过IOS音频开发的同学都知道,Core Audio 是I ...
- Python玩转各种多媒体,视频、音频到图片
我们经常会遇到一些对于多媒体文件修改的操作,像是对视频文件的操作:视频剪辑.字幕编辑.分离音频.视频音频混流等.又比如对音频文件的操作:音频剪辑,音频格式转换.再比如我们最常用的图片文件,格式转换.各 ...
- 【用户交互】APP没有退出前台但改变系统属性如何实时更新UI?监听系统广播,让用户交互更舒心~
前日,一小伙伴问我一个问题,说它解决了半天都没解决这个问题,截图如下: 大概楼主理解如下: 如果在应用中有一个判断wifi的开关和一个当前音量大小的seekbar以及一个获取当前电量多少的按钮,想知道 ...
- Python 资源大全中文版
Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列 ...
随机推荐
- VS2013中的快捷键
在VS2012中Ctrl+E+D 是对齐代码,然而在VS2013中变成了Ctrl+K+F #region的快捷键 Ctrl+K+S /// <summary> 快捷键 先写好函数或方法,然 ...
- 20道C#练习题(一)1——10题
1.输入三个整数,xyz,最终以从小到大的方式输出.利用if嵌套. Console.Write("请输入x="); double x = double.Parse(Console. ...
- mtk的安卓手机刷机时出现的错误信息
手机已成砖,用过好多工具都没刷回来,以下是用smart phone flash tool刷机时出现的错误信息 ---------------------------Smart Phone Flash ...
- 【ubuntu】首选项和应用程序命令(preference & application)
gnome-control-center 系统设置 gnome-control-center region 键盘布局 gnome-control-center screen 屏幕 gno ...
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- install chrome in elementary os
Elementary OS Freya 0.3.2 was officially out for public. As previous release, it comes pre-installed ...
- FastJson之有道翻译
在AndroidMainifast.xml中加入相应的访问权限 <uses-permission android:name="android.permission.INTERNET&q ...
- Objective-C代码的文件扩展名与数据类型
Objective-C数据类型可以分为:基本数据类型.对象类型和id类型. 基本数据类型有:int.float.double和char类型. 对象类型就是类或协议所声明的指针类型,例如:SAutore ...
- zabbix监控mysql主从
最近在公司搭建了zabbix监控系统,现在需要用zabbix来监控mysql的主从同步情况 现在说一下配置的详细步骤: 1.首先给mysql分配一个监控的账号: mysql> grant rep ...
- How To Set Up vsftpd on CentOS 6
About vsftpd 警告:FTP是天生不安全.如果你必须使用FTP,考虑securing your FTP connection with SSL/TLS.否则,最好use SFTP, a se ...