/
mci_cmd.pas
98 lines (85 loc) · 2.21 KB
/
mci_cmd.pas
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
unit mci_cmd;
{Windows標準のMCIを簡単に操作するためのユニット}
interface
uses
Windows,
SysUtils,
Classes,
MMSystem;
function mciCommand(cmd: string): Boolean;
function mciPlayFile(fname: string): Boolean;
function mciPlayFilePos(fname: string; p: Integer): Boolean;
function mciClose: Boolean;
function mciPlayPos: Integer;
function mciPlayLength: Integer;
var
mciMessage,devName: string;
implementation
uses
strunit;
function mciPlayFilePos(fname: string; p: Integer): Boolean;
begin
Result := mciCommand(format('open "%s" alias %s',[fname, devName]));
if Result then
begin
Result := mciCommand('play '+devName+' from '+IntToStr(p));
end;
end;
function mciPlayFile(fname: string): Boolean;
begin
Result := mciCommand(format('open "%s" alias %s',[fname, devName]));
if Result then
begin
Result := mciCommand('play '+devName);
end;
end;
function mciClose: Boolean;
begin
Result := True;
try
Result := mciCommand('close '+devName);
except
end;
end;
function mciPlayPos: Integer;
begin
if mciCommand('status '+devName+' position') then
begin
Result := StrToIntDef(mciMessage,0);
end else
begin
Result := 0;
end;
end;
function mciPlayLength: Integer;
begin
if mciCommand('status '+devName+' length') then
begin
Result := StrToIntDef(mciMessage,0);
end else
begin
Result := 0;
end;
end;
function mciCommand(cmd: string): Boolean;
var
rc: DWORD;
s: string;
begin
Result := True;
SetLength(mciMessage, 512);
rc := mciSendString(PChar(cmd),PChar(mciMessage),Length(mciMessage),0);
if rc<>0 then
begin
SetLength(s,512);
mciGetErrorString(rc, PChar(s), Length(s));
raise EInvalidOperation.Create('MCIのエラー:'#13#10'"'+cmd+'"'+string(PChar(mciMessage))+string(PChar(s)));
end;
end;
initialization
devName := 'mci'+FormatDateTime('hh:nn:ss',Now);
devName := JReplace(devName, '.','_',True);
devName := JReplace(devName, '/','_',True);
devName := JReplace(devName, ':','_',True);
devName := JReplace(devName, ' ','_',True);
end.