forked from Uahh/ToastFish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayMp3.cs
More file actions
197 lines (187 loc) · 6.33 KB
/
Copy pathPlayMp3.cs
File metadata and controls
197 lines (187 loc) · 6.33 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;
namespace ToastFish.Model.Mp3
{
/// <summary>
/// MUSIC 的摘要说明。
/// </summary>
public class MUSIC
{
public MUSIC()
{
}
//定义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 State
{
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 State state;
};
public structMCI mc = new structMCI();
//取得播放文件属性
public string FileName
{
get
{
return mc.iName;
}
set
{
try
{
Name = Name.PadLeft(260, ' ');
mc.iName = value;
ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length);
Name = GetCurrPath(Name);
bool iswave = isFileWave(value);
if (iswave)
{
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " type waveaudio alias media";
}
else
{
Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " type MPEGVideo alias media";
}
//Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media";
ilong = APIClass.mciSendString("close all", IntPtr.Zero, 0, 0);
ilong = APIClass.mciSendString(Name, IntPtr.Zero, 0, 0);
ilong = APIClass.mciSendString("set media time format milliseconds", IntPtr.Zero, 0, 0);
mc.state = State.mStop;
}
catch
{
}
}
}
//播放
public void play()
{
//TemStr = new String('\0', 128);
//APIClass.mciSendString("play media", TemStr, TemStr.Length, 0);
APIClass.mciSendString("play media", IntPtr.Zero, 0, 0);
mc.state = State.mPlaying;
}
//停止
public void StopT()
{
ilong = APIClass.mciSendString("close media", IntPtr.Zero, 0, 0);
ilong = APIClass.mciSendString("close all", IntPtr.Zero, 0, 0);
mc.state = State.mStop;
}
public void Puase()
{
ilong = APIClass.mciSendString("pause media", IntPtr.Zero, 0, 0);
mc.state = State.mPuase;
}
private string GetCurrPath(string name)
{
if (name.Length < 1) return "";
name = name.Trim();
name = name.Substring(0, name.Length - 1);
return name;
}
//总时间
public int Duration
{
get
{
durLength = new String('\0', 128);
APIClass.mciSendString("status media length", durLength, durLength.Length, 0);
durLength = durLength.Trim();
if (durLength == "") return 0;
return (int)(Convert.ToDouble(durLength) / 1000f);
}
}
//当前时间
public int CurrentPosition
{
get
{
durLength = new String('\0', 128);
APIClass.mciSendString("status media position", durLength, durLength.Length, 0);
mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f);
return mc.iPos;
}
}
public bool isFileWave(string path)
{
path = Uri.UnescapeDataString(path);
byte[] b = new byte[16];
bool isSet1 = false;
bool isSet2 = false;
bool isWave = false;
//Read bytes
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
//StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
// FileStream fs = new FileStream(path, FileMode.Open);
//fs.Seek(-128, SeekOrigin.End);
fs.Read(b, 0, 16);
//Set flag
String sFlag = System.Text.Encoding.Default.GetString(b, 0, 4).ToUpper();
String sType = System.Text.Encoding.Default.GetString(b, 8, 4).ToUpper();
if (sFlag.CompareTo("RIFF") == 0) isSet1 = true;
if (sType.CompareTo("WAVE") == 0) isSet2 = true;
if (isSet1 && isSet2)
{
isWave = true;
}
fs.Close();
fs.Dispose();
}
catch (IOException ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
return isWave;
}
}
public class APIClass
{
[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
);
[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
public static extern int mciSendString(
string lpstrCommand,
IntPtr lpstrReturnString,
int uReturnLength,
int hwndCallback
);
}
}