FrmPLSound.cs
3.14 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace GeneralLib
{
public partial class FrmPLSound : Form
{
public FrmPLSound()
{
InitializeComponent();
}
private const int MM_MCINOTIFY = 0x3B9;
[DllImport("winmm.dll")]
private static extern Int32 mciSendString(String command,
StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(string lpszLongPath,
string shortFile,
int cchBuffer);
private void FrmPLSound_Load(object sender, EventArgs e)
{
}
private bool IsLoop = false;
private string PlayKey = "";
private string PlayPath = "";
protected override void DefWndProc(ref Message m)
{
base.DefWndProc(ref m);
if (m.Msg == MM_MCINOTIFY)
{
if (m.WParam == (IntPtr)1)
{
if (IsLoop)
{
Play(PlayKey, PlayPath, true);
}
else
{
StopKey(PlayKey);
//this.Close();
}
}
}
}
private string GetShortPath(string longPath)
{
string shortPath = "";
try
{
shortPath = "".PadLeft(520, Convert.ToChar(" "));
GetShortPathName(longPath, shortPath, shortPath.Length);
int endChr = shortPath.LastIndexOf("\0");
if (endChr >= 0)
shortPath = shortPath.Substring(0, endChr);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
return shortPath;
}
public void Play(string key, string path, bool isLoop)
{
try
{
path = GetShortPath(path);
StopKey(key);
IsLoop = isLoop;
PlayKey = key;
PlayPath = path;
mciSendString("open \"" + path + "\" alias " + key, null, 0, IntPtr.Zero);
mciSendString("play " + key + " notify", null, 0, this.Handle);
}
catch (Exception ex)
{
SystemLog.WriterLog(ex);
}
}
private void StopKey(string key)
{
mciSendString("stop " + key, null, 0, IntPtr.Zero);
mciSendString("close " + key, null, 0, IntPtr.Zero);
}
public void Stop(string key)
{
IsLoop = false;
StopKey(key);
//this.Close();
}
private void FrmPLSound_FormClosing(object sender, FormClosingEventArgs e)
{
IsLoop = false;
StopKey(PlayKey);
}
}
}