C# 使用 NAudio 合并 mp3、wav 音频文件
最编程
2024-05-02 22:10:08
...
public void UploadOrCombineAudioFile()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
if (files == null || files.Count < 1)
return;
string upfileType = HttpContext.Current.Request.Form["upfileType"];//文件格式类型
/*...省略符...*/
int maxSize = 1024 * 1024 * 100;//单文件上传最大限制
List<string> dirDic = new List<string> { ".mp3", ".mp4", ".pcm", ".wav" };//文件格式限制
string uploadPath = HttpContext.Current.Server.MapPath("~/AudioFile/TempFile/");//临时路径
if (!Directory.Exists(uploadPath))
Directory.CreateDirectory(uploadPath);
string fileType = string.Empty;
for (var i = 0; i < files.Count; i++)
{
var postedfile = files[i];
var inputStream = postedfile.InputStream;
if (inputStream.Length > 0)
{
string thisfileName = files[i].FileName;
fileType = Path.GetExtension(thisfileName);
if (postedfile.InputStream == null || postedfile.InputStream.Length > maxSize || !dirDic.Contains(fileType.ToLower()))
return;
/*...我是省略符...*/
int ReadLen = 0;
string uploadFileName = uploadPath + thisfileName;
byte[] bytes = new byte[10240];
using (FileStream stream = new FileStream(uploadFileName, FileMode.OpenOrCreate))//保存文件到指定待合并文件路径下
{
while ((ReadLen = inputStream.Read(bytes, 0, bytes.Length)) > 0)
{
stream.Write(bytes, 0, ReadLen);
}
}
}
}
List<string> filepaths = new List<string>(Directory.GetFiles(uploadPath));//获取待合并文件路径
//文件合并后路径
string savePath = HttpContext.Current.Server.MapPath("~/AudioFile/ComBineFile/" + DateTime.Now.ToString("yyyyMMdd") + "/");
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
/*...我是省略符...*/
byte[] buffer = new byte[10240];
string savefullName = savePath + DateTime.Now.ToString("yyyyMMddHHmmssff") + upfileType;
if (upfileType.ToLower().Equals(".mp3"))
CombineMp3(filepaths, savefullName);//mp3合并
else if ((upfileType.ToLower().Equals(".wav")))
CombineWav(filepaths, savefullName);//wav合并
/*...我是省略符...*/
return;
}