C# 播放 MP3 的資料


在網上找了一些相關的資料,不過暫時還沒時間研究,先存放起來以後有空再研究一下

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
using System;
using System.Text;
using System.Runtime.InteropServices;

namespace Music
{
/**//// <summary>
///
/// </summary>
public class Audio
{
[DllImport("winmm.dll")]
private static extern int mciSendString
(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)] string path,
  [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
  int shortPathLength
);

public Audio()
{

}

public void Play(string FileName)
{
StringBuilder shortPathTemp = new StringBuilder(255);
int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
string ShortPath = shortPathTemp.ToString();

mciSendString("open "+ShortPath+" alias song","",0,0);
mciSendString("play song","",0,0);
}

public void Stop()
{
mciSendString("stop song","",0,0);
}

public void Pause()
{
mciSendString("pause song","",0,0);
}

public void Close()
{
mciSendString("close song","",0,0);
}
}
}
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* // /////希冀媒體播放器類// //////
* /// 版本: 1.0
* /// 此版本為第一個版本,若
* /// 使用中發現問題,請下載
* /// 最新版本,地址:希冀博客
* /// http://ceeji.blog.hexun.com
* /// 版權所有,希冀
* /// 尊重版權,請保留版權信息
*/

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace CeeMediaPlayer
{
/// <summary>
/// 封裝了 WinMM 中的 MciSendString 相關操作。
/// </summary>
public class CeeMediaPlayer
{
//該類包括靜態和動態兩種模式。
/// <summary>
/// 創建 CeeMediaPlayer 的新實例。
/// </summary>
public CeeMediaPlayer()
{
}

//定義API函數使用的字符串變量
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
private string Name = "";

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
private string durLength = "";

[MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)]
private string TemStr = "";

int ilong;

//定義播放狀態枚舉變量
public enum PlayState
{
mPlaying = 1,
mPuase = 2,
mStop = 3
};



//結構變量
public struct structMCI
{
public bool bMut;
public int iDur;
public int iPos;
public int iVol;
public int iBal;
public string iName;
public PlayState state;
};

public structMCI mc = new structMCI();

//取得播放文件屬性
public string FileName
{
get
{
return mc.iName;
}

set
{
//ASCIIEncoding asc = new ASCIIEncoding();
try
{
TemStr = "";
TemStr = TemStr.PadLeft(127, ' ');
Name = Name.PadLeft(260, ' ');
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);

//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";

Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close media", TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0);
mc.state = PlayState.mStop;
}
catch
{
throw new FileNotFoundException(value);
}
}
}



//播放
/// <summary>
/// 播放 this.FileName.
/// </summary>
public void Play()
{
TemStr = "";
TemStr = TemStr.PadLeft(127, Convert.ToChar(" "));
APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
mc.state = PlayState.mPlaying;
}



//停止
/// <summary>
/// 停止播放當前文件。
/// </summary>
public void Stop()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, Convert.ToChar(" "));
ilong = APIClass.mciSendString("stop media", TemStr, 128, 0);
//ilong = APIClass.mciSendString("close all", TemStr, 128, 0);
mc.state = PlayState.mStop;
}

/// <summary>
/// 暫停播放當前文件。
/// </summary>
public void Puase()
{
TemStr = "";
TemStr = TemStr.PadLeft(128, ' ');
ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0);
mc.state = PlayState.mPuase;
}

private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}

//總時間
/// <summary>
/// 獲得當前文件總播放時間,單位為毫秒。
/// </summary>
public int Duration
{
get
{
if (mc.state == PlayState.mStop)
{
}
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();

if (durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}

/// <summary>
/// 獲取當前所在的播放位置。
/// </summary>

public int CurrentPosition
{
get
{
durLength = "";
durLength = durLength.PadLeft(128, Convert.ToChar(" "));
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}

}

/// <summary>
/// 提供用戶和 WinMM 對象的直接交互。
/// </summary>

public class APIClass
{

/// <summary>
/// 獲取指定文件的短名稱。
/// </summary>
/// <param name="lpszLongPath">長名稱</param>
/// <param name="shortFile">返回的短名稱</param>
/// <param name="cchBuffer">緩衝區</param>
/// <returns></returns>

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName(
string lpszLongPath,
string shortFile,
int cchBuffer
);

[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}

調整音量 :

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace waveOutSetVolume
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("winmm.dll")] //引用winmm.dll

public static extern long waveOutSetVolume(uint deviceID, uint Volume); //參數為uint
private void Form1_Load(object sender, EventArgs e)
{

}

// trackBar 控件的最大值為0xffff,最小值為0x0000
private void trackBar1_Scroll(object sender, EventArgs e)
{
waveOutSetVolume(0, Convert.ToUInt16(trackBar1.Value));
}
}
}