本文是采用Windows Media Player控件,如果工具箱中没有,请点击工具——》选择工具箱——》COM组件进行查找,打钩,确定。即可在工具箱中找到。我们将采用的音频格式是wav格式。下边是窗口展示。
下边是代码实现
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
if(openFileDialog1 .ShowDialog ()==DialogResult.OK )
{
axWindowsMediaPlayer1.URL = openFileDialog1.FileName;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
int begin = Convert.ToInt32(tbStartTime.Text);
int end = Convert.ToInt32(tbEndTime.Text);
string source = "C:\\Users\\Administrator.USER-20160718BV\\Desktop\\林俊杰 - 可惜没如果.wav";
string dest = "C:\\Users\\Administrator.USER-20160718BV\\Desktop\\音频q.wav";
string errorInfo = "";
WavScissorHelper.GetWavFileScissor(source, dest, begin, end,ref errorInfo);
}
class WavScissorHelper
{
/// <summary>
/// 文件截取处理
/// </summary>
public static bool GetWavFileScissor(string OriginalAudioFilePath, string DestinedAudioFilePath, int beginCutTime, int endCutTime, ref string errorInfo)
{
if (!File.Exists(OriginalAudioFilePath))
{
errorInfo = string.Format("源文件:[{0}]不存在!", OriginalAudioFilePath);
return false;
}
if (!Directory.Exists(Path.GetDirectoryName(DestinedAudioFilePath)))
{
errorInfo = string.Format("文件夹:[{0}]不存在!", Path.GetDirectoryName(DestinedAudioFilePath));
return false;
}
if ((beginCutTime < 0) || (endCutTime < 0)
|| (beginCutTime > endCutTime))
{
errorInfo = string.Format("截取时间[{0}][{1}]有误!", beginCutTime, endCutTime);
return false;
}
try
{