使用Delphi中System中的MediaPlayer控件。
因为我主要是需要使用播放声频文件中的片断,比如 21000ms ~35000ms。在Delphi中可以使用MediaPlayer的StartPos和EndPos来实现。
在编程过程中主要遇到的问题是:当播放wav格式时,利用设置StartPos和EndPos可以播放任意位置的片断,但是当播放mp3时,StartPos有效,而EndPos属性无效。
//用来监控mp3文件结尾的Timer,时间间隔要设为100ms
procedure TMyPlayer.EndPosTimerTimer(Sender: TObject);
begin
with mp do begin
if Position > EndPos then begin
// 停止播放
Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := StartPos;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;
end;
end;
另外,这个示例中还利用TrackBar和另一个Timer实现了播放的进度条。
//用来控制播放进度条的Timer,时间间隔要为100ms
procedure TMyPlayer.TrackBarTimerTimer(Sender: TObject);
begin
with SoundBar do begin
Min := mp.StartPos;
Max := mp.EndPos;
Frequency := 50;
Position := mp.Position;
end;
end;
整个pas文件的的代码如下:
unit mp3main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MPlayer, StdCtrls, ExtCtrls, ComCtrls;

type
TMyPlayer = class(TForm)
mp: TMediaPlayer;
Play: TButton;
info: TMemo;
Change: TButton;
Stop: TButton;
EndPosTimer: TTimer;
SoundBar: TTrackBar;
TrackBarTimer: TTimer;
procedure PlayClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ChangeClick(Sender: TObject);
procedure StopClick(Sender: TObject);
procedure EndPosTimerTimer(Sender: TObject);
procedure TrackBarTimerTimer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MyPlayer: TMyPlayer;

implementation

{$R *.dfm}

procedure TMyPlayer.PlayClick(Sender: TObject);
var sWinDir: String;
sFile: String;
begin
// Initialize Variable
sWinDir := 'E:/testapp/audio/';
info.Lines.Append(sWinDir);

// 先判断是否已经在播放了
if mp.Mode = mpPlaying then
mp.Stop;
mp.Close;

// 设置播放的文件
sFile := sWinDir + 'audiowinter.mp3';

// =========== 判断文件是否存在 ===========
if not FileExists(sFile) then
begin
info.Lines.Append(sFile + ' 不存在.');
ShowMessage('文件不存在');
exit;
end;

// 播放音频文件
try
with mp do begin

Filename := sFile; //specify video file
DeviceType := dtAutoSelect;
TimeFormat := tfMilliseconds;

Open;

StartPos := 10000;
EndPos := StartPos + 10000;

Play;
EndPosTimer.Enabled := true;
TrackBarTimer.Enabled := true;
info.Lines.Append('StartPos: ' + IntToStr(StartPos) + 'ms');
info.Lines.Append('EndPos: ' + IntToStr(EndPos) + 'ms');
info.Lines.Append('Started at ' + IntToStr(Position) + 'ms');

Change.Enabled := true;

end;

//info.Text := info.Text + ' ' + String(mp.Position);

finally
Screen.Cursor := crArrow;
end;
end;

procedure TMyPlayer.FormCreate(Sender: TObject);
begin
info.Text := '';
end;

// =========== 随机读文件 ===========
procedure TMyPlayer.ChangeClick(Sender: TObject);
begin
with mp do begin
if Mode = mpPlaying then Pause;
StartPos := 2000; //设置开始播放的位置
EndPos := StartPos + 10000;

Play;
EndPosTimer.Enabled := true;
TrackBarTimer.Enabled := true;
info.Lines.Append('StartPos: ' + IntToStr(StartPos) + 'ms');
info.Lines.Append('EndPos: ' + IntToStr(EndPos) + 'ms');
info.Lines.Append('Started at ' + IntToStr(Position) + 'ms');
end;
end;

procedure TMyPlayer.StopClick(Sender: TObject);
begin
// info.Text := info.Text + ' ' + String(mp.Position);
if mp.Mode = mpPlaying then begin
// 停止播放
mp.Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := mp.StartPos;
end;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;

//用来监控mp3文件结尾的Timer,时间间隔要设为100ms
procedure TMyPlayer.EndPosTimerTimer(Sender: TObject);
begin
with mp do begin
if Position > EndPos then begin
// 停止播放
Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := StartPos;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;
end;
end;

//用来控制播放进度条的Timer,时间间隔要为100ms
procedure TMyPlayer.TrackBarTimerTimer(Sender: TObject);
begin
with SoundBar do begin
Min := mp.StartPos;
Max := mp.EndPos;
Frequency := 50;
Position := mp.Position;
end;
end;

end.
因为我主要是需要使用播放声频文件中的片断,比如 21000ms ~35000ms。在Delphi中可以使用MediaPlayer的StartPos和EndPos来实现。
在编程过程中主要遇到的问题是:当播放wav格式时,利用设置StartPos和EndPos可以播放任意位置的片断,但是当播放mp3时,StartPos有效,而EndPos属性无效。
如下代码:后来没办法,只有使用一个Timer来监控播放的情况,当mp.Postion > mp.EndPos时,就停止播放。为了控制的精确性,需要将Timer的间隔Interval设置为100ms或更小的数值。with mp do begin
Filename := sFile;
DeviceType := dtAutoSelect;
TimeFormat := tfMilliseconds;
Open;
StartPos := 10000;
EndPos := StartPos + 2000;
Play;
end;
当 sFile 是wav格式时,一点问题都没有,刚好播放了2s的内容,但是当格式为mp3时,
StartPos有效,从10s开始播放,但EndPos无效,停不下来。
//用来监控mp3文件结尾的Timer,时间间隔要设为100ms
procedure TMyPlayer.EndPosTimerTimer(Sender: TObject);
begin
with mp do begin
if Position > EndPos then begin
// 停止播放
Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := StartPos;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;
end;
end;另外,这个示例中还利用TrackBar和另一个Timer实现了播放的进度条。
//用来控制播放进度条的Timer,时间间隔要为100ms
procedure TMyPlayer.TrackBarTimerTimer(Sender: TObject);
begin
with SoundBar do begin
Min := mp.StartPos;
Max := mp.EndPos;
Frequency := 50;
Position := mp.Position;
end;
end;整个pas文件的的代码如下:
unit mp3main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MPlayer, StdCtrls, ExtCtrls, ComCtrls;
type
TMyPlayer = class(TForm)
mp: TMediaPlayer;
Play: TButton;
info: TMemo;
Change: TButton;
Stop: TButton;
EndPosTimer: TTimer;
SoundBar: TTrackBar;
TrackBarTimer: TTimer;
procedure PlayClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ChangeClick(Sender: TObject);
procedure StopClick(Sender: TObject);
procedure EndPosTimerTimer(Sender: TObject);
procedure TrackBarTimerTimer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MyPlayer: TMyPlayer;
implementation
{$R *.dfm}
procedure TMyPlayer.PlayClick(Sender: TObject);
var sWinDir: String;
sFile: String;
begin
// Initialize Variable
sWinDir := 'E:/testapp/audio/';
info.Lines.Append(sWinDir);
// 先判断是否已经在播放了
if mp.Mode = mpPlaying then
mp.Stop;
mp.Close;
// 设置播放的文件
sFile := sWinDir + 'audiowinter.mp3';
// =========== 判断文件是否存在 ===========
if not FileExists(sFile) then
begin
info.Lines.Append(sFile + ' 不存在.');
ShowMessage('文件不存在');
exit;
end;
// 播放音频文件
try
with mp do begin
Filename := sFile; //specify video file
DeviceType := dtAutoSelect;
TimeFormat := tfMilliseconds;
Open;
StartPos := 10000;
EndPos := StartPos + 10000;
Play;
EndPosTimer.Enabled := true;
TrackBarTimer.Enabled := true;
info.Lines.Append('StartPos: ' + IntToStr(StartPos) + 'ms');
info.Lines.Append('EndPos: ' + IntToStr(EndPos) + 'ms');
info.Lines.Append('Started at ' + IntToStr(Position) + 'ms');
Change.Enabled := true;
end;
//info.Text := info.Text + ' ' + String(mp.Position);
finally
Screen.Cursor := crArrow;
end;
end;
procedure TMyPlayer.FormCreate(Sender: TObject);
begin
info.Text := '';
end;
// =========== 随机读文件 ===========
procedure TMyPlayer.ChangeClick(Sender: TObject);
begin
with mp do begin
if Mode = mpPlaying then Pause;
StartPos := 2000; //设置开始播放的位置
EndPos := StartPos + 10000;
Play;
EndPosTimer.Enabled := true;
TrackBarTimer.Enabled := true;
info.Lines.Append('StartPos: ' + IntToStr(StartPos) + 'ms');
info.Lines.Append('EndPos: ' + IntToStr(EndPos) + 'ms');
info.Lines.Append('Started at ' + IntToStr(Position) + 'ms');
end;
end;
procedure TMyPlayer.StopClick(Sender: TObject);
begin
// info.Text := info.Text + ' ' + String(mp.Position);
if mp.Mode = mpPlaying then begin
// 停止播放
mp.Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := mp.StartPos;
end;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;
//用来监控mp3文件结尾的Timer,时间间隔要设为100ms
procedure TMyPlayer.EndPosTimerTimer(Sender: TObject);
begin
with mp do begin
if Position > EndPos then begin
// 停止播放
Stop;
// 关闭两个Timer
EndPosTimer.Enabled := false;
TrackBarTimer.Enabled := false;
// 把进度条设到开始
SoundBar.Position := StartPos;
info.Lines.Append('Stopped at ' + IntToStr(mp.Position) + 'ms');
end;
end;
end;
//用来控制播放进度条的Timer,时间间隔要为100ms
procedure TMyPlayer.TrackBarTimerTimer(Sender: TObject);
begin
with SoundBar do begin
Min := mp.StartPos;
Max := mp.EndPos;
Frequency := 50;
Position := mp.Position;
end;
end;
end.
本文介绍了如何使用Delphi的System组件中的MediaPlayer控件来创建一个简单的音频播放器,特别是如何处理wav和mp3文件的片段播放。在处理mp3文件时,由于EndPos属性对mp3无效,因此采用了一个Timer来监控并手动停止播放。

TMyPlayer


被折叠的 条评论
为什么被折叠?



