今天在處理微信的開發接口時候,發現微信多媒體上傳接口中返回的音頻格式是amr。坑人的是現在大部分的web 播放器,不支持amr的格式播放。試了很多方法都不行。
沒辦法,只要找一個妥協的解決方案:將amr轉換成mp3再進行播放。
於是,我在網上找了不少的音頻轉換方案,其中有DIrectshow和 ffmpeg。DIrectshow使用起來比較復雜,我就采用ffmpeg。通過調用ffmpeg.exe傳遞命令參數的方式,進行音頻格式的轉換。
該程序,主要是封裝了調用ffmpeg.exe,並執行命令參數的功能。
核心代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
namespace
WavConvertAmr
{
public
class
WavConvertToAmr
{
/// <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 12.2k -ac 1 "
+ 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
{ }
}
/// <summary>
/// 獲取文件的byte[]
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public
byte
[] GetFileByte(
string
fileName)
{
FileStream pFileStream =
null
;
byte
[] pReadByte =
new
byte
[0];
try
{
pFileStream =
new
FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r =
new
BinaryReader(pFileStream);
r.BaseStream.Seek(0, SeekOrigin.Begin);
//將文件指針設置到文件開
pReadByte = r.ReadBytes((
int
)r.BaseStream.Length);
return
pReadByte;
}
catch
{
return
pReadByte;
}
finally
{
if
(pFileStream !=
null
)
pFileStream.Close();
}
}
/// <summary>
/// 將文件的byte[]生成文件
/// </summary>
/// <param name="pReadByte"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public
bool
writeFile(
byte
[] pReadByte,
string
fileName)
{
FileStream pFileStream =
null
;
try
{
pFileStream =
new
FileStream(fileName, FileMode.OpenOrCreate);
pFileStream.Write(pReadByte, 0, pReadByte.Length);
}
catch
{
return
false
;
}
finally
{
if
(pFileStream !=
null
)
pFileStream.Close();
}
return
true
;
}
}
}
|
調用代碼:
1
2
3
4
5
6
7
8
9
10
11
12
|
private
void
button3_Click(
object
sender, EventArgs e)
{
OpenFileDialog open =
new
OpenFileDialog();
if
(open.ShowDialog() == DialogResult.OK)
{
string
fileName = open.FileName;
string
targetFileName =
"C:\\2222.mp3"
;
WavConvertAmr.WavConvertToAmr toamr =
new
WavConvertAmr.WavConvertToAmr();
toamr.ConvertToAmr(System.Windows.Forms.Application.StartupPath, fileName, targetFileName);
MessageBox.Show(
"轉換成功"
);
}
}
|
示例下載:
鏈接:http://pan.baidu.com/s/1jGxa4M6 密碼:bnqw