我看delphi4.0入门与提高上关于mediaplayer的一个例子,播放一个delphi自带的AVI的例子。
这个例子在TPanel上显示播放的文件。
使用列表框选择设备类型,filename文本框显示媒体文件名,单击loadbtn按钮打开opendialog选择打开的文件,使用open按钮打开媒体文件。打开媒体设备后使用几个按钮就可控制图像的播放。同时,trackbar及时的显示播放进度。
下面是代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.ItemIndex:=0;
mediaplayer1.Display:=displaypanel;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
begin
mediaplayer1.Close;
case combobox1.ItemIndex of
0:begin
mediaplayer1.DeviceType:=dtautoselect;
opendialog1.Filterindex:=5
end;
1:begin
mediaplayer1.DeviceType:=dtavivideo;
opendialog1.Filterindex:=3
end;
2:begin
mediaplayer1.DeviceType:=dtcdaudio;
end;
3:begin
mediaplayer1.DeviceType:=dtdat;
opendialog1.Filterindex:=4
end;
4:begin
mediaplayer1.DeviceType:=dtsequencer;
opendialog1.Filterindex:=2
end;
5:begin
mediaplayer1.DeviceType:=dtwaveaudio;
opendialog1.Filterindex:=1
end
end
end;procedure TForm1.openClick(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then mediaplayer1.Close;
if mediaplayer1.DeviceType<>dtCDAudio then mediaplayer1.FileName:=filename.Text;
mediaplayer1.Open;
mediaplayer1.DisplayRect:=displaypanel.ClientRect;
end;procedure TForm1.displaypanelClick(Sender: TObject);
begin
mediaplayer1.DisplayRect:=displaypanel.ClientRect;
end;procedure TForm1.loadbtnClick(Sender: TObject);
begin
if opendialog1.Execute then
filename.Text:=opendialog1.FileName;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then
begin
mediaplayer1.TimeFormat:=tfmilliseconds;
trackbar1.Max:= mediaplayer1.Length div 1000;
trackbar1.position:= mediaplayer1.Position div 1000;
end
end;procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then
begin
mediaplayer1.Position:=trackbar1.Position*1000;
mediaplayer1.Play;
end
end;请帮我看看,到底是为什么不播放?我也不知道在TPanel上是否能播放avi文件,可例子就是这么做的。
这个例子在TPanel上显示播放的文件。
使用列表框选择设备类型,filename文本框显示媒体文件名,单击loadbtn按钮打开opendialog选择打开的文件,使用open按钮打开媒体文件。打开媒体设备后使用几个按钮就可控制图像的播放。同时,trackbar及时的显示播放进度。
下面是代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.ItemIndex:=0;
mediaplayer1.Display:=displaypanel;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
begin
mediaplayer1.Close;
case combobox1.ItemIndex of
0:begin
mediaplayer1.DeviceType:=dtautoselect;
opendialog1.Filterindex:=5
end;
1:begin
mediaplayer1.DeviceType:=dtavivideo;
opendialog1.Filterindex:=3
end;
2:begin
mediaplayer1.DeviceType:=dtcdaudio;
end;
3:begin
mediaplayer1.DeviceType:=dtdat;
opendialog1.Filterindex:=4
end;
4:begin
mediaplayer1.DeviceType:=dtsequencer;
opendialog1.Filterindex:=2
end;
5:begin
mediaplayer1.DeviceType:=dtwaveaudio;
opendialog1.Filterindex:=1
end
end
end;procedure TForm1.openClick(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then mediaplayer1.Close;
if mediaplayer1.DeviceType<>dtCDAudio then mediaplayer1.FileName:=filename.Text;
mediaplayer1.Open;
mediaplayer1.DisplayRect:=displaypanel.ClientRect;
end;procedure TForm1.displaypanelClick(Sender: TObject);
begin
mediaplayer1.DisplayRect:=displaypanel.ClientRect;
end;procedure TForm1.loadbtnClick(Sender: TObject);
begin
if opendialog1.Execute then
filename.Text:=opendialog1.FileName;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then
begin
mediaplayer1.TimeFormat:=tfmilliseconds;
trackbar1.Max:= mediaplayer1.Length div 1000;
trackbar1.position:= mediaplayer1.Position div 1000;
end
end;procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if mediaplayer1.mode=mpplaying then
begin
mediaplayer1.Position:=trackbar1.Position*1000;
mediaplayer1.Play;
end
end;请帮我看看,到底是为什么不播放?我也不知道在TPanel上是否能播放avi文件,可例子就是这么做的。
begin
if mediaplayer1.mode=mpplaying then mediaplayer1.Close;
if mediaplayer1.DeviceType<>dtCDAudio then mediaplayer1.FileName:=filename.Text;
mediaplayer1.Open;
mediaplayer1.play;//这句不能少....
mediaplayer1.DisplayRect:=displaypanel.ClientRect;
end;
你试试!
是不是用TPanel显示avi文件有问题?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, MPlayer;type
TForm1 = class(TForm)
MediaPlayer1: TMediaPlayer;
Button1: TButton;
Panel1: TPanel;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
MediaPlayer1.FileName :=OpenDialog1.FileName ;
MediaPlayer1.Open ;
MediaPlayer1.Play ;
MediaPlayer1.DisplayRect:=panel1.ClientRect;
end;end.临时做的