Delphi播放声音文件类(包括调整其中任意一个声音的大小)
2010-09-19 21:02
381 查看
自行封装的播放声音类文件 TSoundPlayer , 用MCI播放的。
但是TSoundPlayer类无法提供针对每个声音的大小调节,比如一个背景音乐,一个前景音乐。
为此,我安装了MMTools2.0,在窗体上放上MidiPlayBg:TMMDSMidiChannel控件和TMMDSWaveMixer控件,
并且进行关联,就可以调用TMMDSMidiChannel.Volume来设置音量了MidiPlayBg.Volume := -1500; 在这里,
我的背景音乐是Midi文件。
MMTools官方网站为:http://www.swiftsoft.de/
类文件:
{ 播放声音; 有相对目录; 可以播放列表; 可以循环播放 }
unit SoundPlayer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Controls,Graphics, MPlayer;
type
// 播放方式( sptPlayAll - 全部播放一遍; sptLoopAll - 全部循环播放)
TSoundPlayType = (sptPlayAll, sptLoopAll);
TSoundPlayer = class(TMediaPlayer)
private
FBaseDir: string; // 基本路径一定以 /结束
FPlayFileList: TStringList; // 相对 FBaseDir的路径
FPlayType: TSoundPlayType; // 播放方式
FCurPlayIndex: integer; // 当前播放的文件 -1表示没有
procedure SetBaseDir(const Value: string);
procedure SetFileList(const Value: TStringList);
procedure SetPlayType(const Value: TSoundPlayType);
procedure MPOnNotify(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// AddFile - 添加一个文件
// SetFile - 修改指定位置文件
// ClearFile - 清空文件列表
procedure AddFile(const vFileName: string);
procedure SetFile(const vIndex: integer; const vFileName: string);
procedure ClearFile;
// 播放单个文件
procedure AddSingleFile(const AFileName: string);
// 开始和结束播放
procedure StartPlay;
procedure StopPlay;
property BaseDir: string read FBaseDir write SetBaseDir;
property PlayFileList: TStringList read FPlayFileList write SetFileList;
property PlayType: TSoundPlayType read FPlayType write SetPlayType;
end;
implementation
{ TSoundPlayer }
procedure TSoundPlayer.AddFile(const vFileName: string);
var
vFile: string;
begin
//如果不用System, 会冲突
if System.Length(vFileName) = 0 then
exit;
vFile := FBaseDir + vFileName;
if not FileExists(vFile) then
exit;
FPlayFileList.Add(vFileName);
end;
procedure TSoundPlayer.ClearFile;
begin
StopPlay;
FPlayFileList.Clear;
end;
constructor TSoundPlayer.Create(AOwner: TComponent);
begin
inherited;
AutoOpen := false;
DeviceType := dtAutoSelect;
Notify := False; //如果 Notify=true, Open也通知成功
OnNotify := MPOnNotify;
Visible := false;
Height := 0;
Width := 0;
FBaseDir := '';
FPlayFileList := TStringList.Create;
FPlayFileList.Clear;
FPlayType := sptPlayAll;
FCurPlayIndex := -1;
end;
destructor TSoundPlayer.Destroy;
begin
StopPlay;
if Assigned(FPlayFileList) then
FreeAndNil(FPlayFileList);
inherited;
end;
procedure TSoundPlayer.MPOnNotify(Sender: TObject);
begin
//播放完毕
if NotifyValue = nvSuccessful then
begin
//没有开始则退出
if FCurPlayIndex < 0 then
Exit;
case FPlayType of
sptPlayAll:
begin
Inc(FCurPlayIndex);
//如果播放完毕
if FCurPlayIndex >= FPlayFileList.Count then begin
StopPlay;
Exit;
end;
end;
sptLoopAll:
begin
Inc(FCurPlayIndex);
if FCurPlayIndex >= FPlayFileList.Count then
begin
FCurPlayIndex := 0;
//如果列表已经清空
if FPlayFileList.Count = 0 then
begin
StopPlay;
Exit;
end;
end;
end;
end;
//开始下一个
FileName := FBaseDir + FPlayFileList.Strings[FCurPlayIndex];
try Open; except StopPlay; end;
Play;
end;
end;
procedure TSoundPlayer.SetBaseDir(const Value: string);
var
Len: integer;
begin
if FBaseDir = Value then
exit;
if not DirectoryExists(Value) then
exit;
StopPlay;
FBaseDir := Value;
Len := System.Length(FBaseDir);
if FBaseDir[Len] <> '/' then
FBaseDir := FBaseDir + '/';
end;
procedure TSoundPlayer.SetFile(const vIndex: integer;
const vFileName: string);
begin
if vIndex < 0 then
exit;
if vIndex >= FPlayFileList.Count then
exit;
if not FileExists(FBaseDir + vFileName) then
exit;
if FPlayFileList.Strings[vIndex] = vFileName then
exit;
StopPlay;
FPlayFileList.Strings[vIndex] := vFileName;
end;
procedure TSoundPlayer.SetFileList(const Value: TStringList);
begin
if not Assigned(Value) then
exit;
StopPlay;
FPlayFileList.Clear;
FPlayFileList.Assign(Value);
end;
procedure TSoundPlayer.SetPlayType(const Value: TSoundPlayType);
begin
if FPlayType = Value then
exit;
StopPlay;
FPlayType := Value;
end;
procedure TSoundPlayer.AddSingleFile(const AFileName: string);
begin
if System.Length(AFileName) = 0 then
Exit;
ClearFile;
AddFile(AFileName);
end;
procedure TSoundPlayer.StartPlay;
begin
if not DirectoryExists(FBaseDir) then
Exit;
if FPlayFileList.Count < 1 then
Exit;
StopPlay;
FCurPlayIndex := 0;
FileName := FBaseDir + FPlayFileList.Strings[FCurPlayIndex];
try
Open;
except
StopPlay;
end;
Play;
end;
procedure TSoundPlayer.StopPlay;
begin
case Self.Mode of
mpPlaying: begin
Stop;
Close;
end;
mpRecording: begin
Stop;
Close;
end;
mpSeeking: begin
Stop;
Close;
end;
mpOpen: begin
Close;
end;
end;
FCurPlayIndex := -1;
end;
end.
但是TSoundPlayer类无法提供针对每个声音的大小调节,比如一个背景音乐,一个前景音乐。
为此,我安装了MMTools2.0,在窗体上放上MidiPlayBg:TMMDSMidiChannel控件和TMMDSWaveMixer控件,
并且进行关联,就可以调用TMMDSMidiChannel.Volume来设置音量了MidiPlayBg.Volume := -1500; 在这里,
我的背景音乐是Midi文件。
MMTools官方网站为:http://www.swiftsoft.de/
类文件:
{ 播放声音; 有相对目录; 可以播放列表; 可以循环播放 }
unit SoundPlayer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Controls,Graphics, MPlayer;
type
// 播放方式( sptPlayAll - 全部播放一遍; sptLoopAll - 全部循环播放)
TSoundPlayType = (sptPlayAll, sptLoopAll);
TSoundPlayer = class(TMediaPlayer)
private
FBaseDir: string; // 基本路径一定以 /结束
FPlayFileList: TStringList; // 相对 FBaseDir的路径
FPlayType: TSoundPlayType; // 播放方式
FCurPlayIndex: integer; // 当前播放的文件 -1表示没有
procedure SetBaseDir(const Value: string);
procedure SetFileList(const Value: TStringList);
procedure SetPlayType(const Value: TSoundPlayType);
procedure MPOnNotify(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// AddFile - 添加一个文件
// SetFile - 修改指定位置文件
// ClearFile - 清空文件列表
procedure AddFile(const vFileName: string);
procedure SetFile(const vIndex: integer; const vFileName: string);
procedure ClearFile;
// 播放单个文件
procedure AddSingleFile(const AFileName: string);
// 开始和结束播放
procedure StartPlay;
procedure StopPlay;
property BaseDir: string read FBaseDir write SetBaseDir;
property PlayFileList: TStringList read FPlayFileList write SetFileList;
property PlayType: TSoundPlayType read FPlayType write SetPlayType;
end;
implementation
{ TSoundPlayer }
procedure TSoundPlayer.AddFile(const vFileName: string);
var
vFile: string;
begin
//如果不用System, 会冲突
if System.Length(vFileName) = 0 then
exit;
vFile := FBaseDir + vFileName;
if not FileExists(vFile) then
exit;
FPlayFileList.Add(vFileName);
end;
procedure TSoundPlayer.ClearFile;
begin
StopPlay;
FPlayFileList.Clear;
end;
constructor TSoundPlayer.Create(AOwner: TComponent);
begin
inherited;
AutoOpen := false;
DeviceType := dtAutoSelect;
Notify := False; //如果 Notify=true, Open也通知成功
OnNotify := MPOnNotify;
Visible := false;
Height := 0;
Width := 0;
FBaseDir := '';
FPlayFileList := TStringList.Create;
FPlayFileList.Clear;
FPlayType := sptPlayAll;
FCurPlayIndex := -1;
end;
destructor TSoundPlayer.Destroy;
begin
StopPlay;
if Assigned(FPlayFileList) then
FreeAndNil(FPlayFileList);
inherited;
end;
procedure TSoundPlayer.MPOnNotify(Sender: TObject);
begin
//播放完毕
if NotifyValue = nvSuccessful then
begin
//没有开始则退出
if FCurPlayIndex < 0 then
Exit;
case FPlayType of
sptPlayAll:
begin
Inc(FCurPlayIndex);
//如果播放完毕
if FCurPlayIndex >= FPlayFileList.Count then begin
StopPlay;
Exit;
end;
end;
sptLoopAll:
begin
Inc(FCurPlayIndex);
if FCurPlayIndex >= FPlayFileList.Count then
begin
FCurPlayIndex := 0;
//如果列表已经清空
if FPlayFileList.Count = 0 then
begin
StopPlay;
Exit;
end;
end;
end;
end;
//开始下一个
FileName := FBaseDir + FPlayFileList.Strings[FCurPlayIndex];
try Open; except StopPlay; end;
Play;
end;
end;
procedure TSoundPlayer.SetBaseDir(const Value: string);
var
Len: integer;
begin
if FBaseDir = Value then
exit;
if not DirectoryExists(Value) then
exit;
StopPlay;
FBaseDir := Value;
Len := System.Length(FBaseDir);
if FBaseDir[Len] <> '/' then
FBaseDir := FBaseDir + '/';
end;
procedure TSoundPlayer.SetFile(const vIndex: integer;
const vFileName: string);
begin
if vIndex < 0 then
exit;
if vIndex >= FPlayFileList.Count then
exit;
if not FileExists(FBaseDir + vFileName) then
exit;
if FPlayFileList.Strings[vIndex] = vFileName then
exit;
StopPlay;
FPlayFileList.Strings[vIndex] := vFileName;
end;
procedure TSoundPlayer.SetFileList(const Value: TStringList);
begin
if not Assigned(Value) then
exit;
StopPlay;
FPlayFileList.Clear;
FPlayFileList.Assign(Value);
end;
procedure TSoundPlayer.SetPlayType(const Value: TSoundPlayType);
begin
if FPlayType = Value then
exit;
StopPlay;
FPlayType := Value;
end;
procedure TSoundPlayer.AddSingleFile(const AFileName: string);
begin
if System.Length(AFileName) = 0 then
Exit;
ClearFile;
AddFile(AFileName);
end;
procedure TSoundPlayer.StartPlay;
begin
if not DirectoryExists(FBaseDir) then
Exit;
if FPlayFileList.Count < 1 then
Exit;
StopPlay;
FCurPlayIndex := 0;
FileName := FBaseDir + FPlayFileList.Strings[FCurPlayIndex];
try
Open;
except
StopPlay;
end;
Play;
end;
procedure TSoundPlayer.StopPlay;
begin
case Self.Mode of
mpPlaying: begin
Stop;
Close;
end;
mpRecording: begin
Stop;
Close;
end;
mpSeeking: begin
Stop;
Close;
end;
mpOpen: begin
Close;
end;
end;
FCurPlayIndex := -1;
end;
end.
相关文章推荐
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题
- delphi如何加上spliter分割条,任意调整大小
- 牛牛和15朋友来玩打土豪分田地的游戏,牛牛决定让你来分田地,地主的田地可以看成是一个矩形,每个位置有一个价值。分割田地的方法是横竖各切三刀,分成26份,作为领导干部,牛牛总是会选择其中总价值最小的一份田地,作为牛牛最好的朋友,你希望牛牛取得的田地价值和尽可能大,你知道这个值最大可以是多少吗? 输入两个整数n和m(1≤n,m≤75)表示田地的大小,接下来n行,每行包括m个0-9之间的数字,表示每块
- delphi如何加上spliter分割条,任意调整大小?
- 输入任意大小的三个整数,判断其中是否有两个奇数一个偶数。若是,则输出“YES”,不是则输出“NOT” (要求用条件表达式进行判断,不使用if语句)
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题,暂停其他的播放
- android中一个TextView实现其中内容大小颜色不同的效果
- 要对一个任意进程(包括系统安全进程和服务进程)进行指定了相关的访问权 [OpenProcessToken,LookupPrivilegeValue,AdjustTokenPrivileges]
- TurnSharp可以把任意delphi转为C#2.0的代码,包括第三方组件
- --==vb6中用图片框任意大小播放AVI电影(New)==--
- 3370 Halloween treats 抽屉原理 给定n个数,求其中的任意一个子集满足集合中的每个元素值加和正好是c的倍数
- delphi 播放wav声音
- 对于一个n位正整数a,去掉其中任意k(k<=n)个数字后,剩下的数字按原次序排列可以组成一个新的正整数。设计一个删数算法,使得剩下的数字组成的正整数最小。例如,a=13243221,k=5,输出:12
- 输入两个整数序列。其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序。 为了简单起见,我们假设push序列的任意两个整数都是不相等的
- android静音模式下仍可以播放媒体声音以及调节其大小。。。。
- 实现Android播放声音资源的一个简单的工具类
- 一个调整应用图标大小的小工具
- 创建一个模块calculator.py,完成任意两个数的加(add)、减(sub)、乘(mult)、除(div)运算;导入该模块,分别调用其中的函数,完成如下操作: 1、25+56 2、86-68 3