请教各位大侠,如何获取媒体播放时间?(200分)

  • 主题发起人 主题发起人 hyserver
  • 开始时间 开始时间
H

hyserver

Unregistered / Unconfirmed
GUEST, unregistred user!
我自己做了个简易播放器,要怎样才能够获取播放文件的时间长度呢,我自己写了一段代码但是获取时间不对,下面我把源代码全部给出来,请各位大侠指点小弟一下。最好可以指点一下小弟,如何能够实现播放器的全部够功能,比如全屏的实现和转换,拖放等等!
 
大哥,不是说有代码的吗?
你用什么办法播放的呀?(MediaPlayer, MCI, DirectShow),每种方法解决方式不同的.
 
unit Unit1;

interface

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

type
HMSRec=record
Hours: byte;
Minutes: byte;
Seconds: byte;
Notused: byte;
end;


TForm1 = class(TForm)
Panel1: TPanel;
MediaPlayer1: TMediaPlayer;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
TrackBar1: TTrackBar;
OpenDialog1: TOpenDialog;
MainMenu1: TMainMenu;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
Label1: TLabel;
Timer1: TTimer;
procedure N1Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);

private
CanTimerAutoChange:Boolean;
{ Private declarations }
public

{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.N1Click(Sender: TObject);
begin

Form2.BorderStyle:=bsNone;
form2.WindowState:=wsMaximized;

MediaPlayer1.Display:=form2;

MediaPlayer1.DisplayRect:=Rect(0,0,Form2.Width,Form2.Height);



end;



procedure TForm1.TrackBar1Change(Sender: TObject);
var t,v:longint;
begin

t:=TrackBar1.position;
v:=(t shl 8) or (t shl 24);
WaveOutSetvolume(0,v);
end;


procedure TForm1.N4Click(Sender: TObject);

begin

if OpenDialog1.Execute then

begin

MediaPlayer1.FileName:= OpenDialog1.FileName;
MediaPlayer1.Open;
MediaPlayer1.Notify:=true;
end;


end;


procedure TForm1.N5Click(Sender: TObject);
begin

Close;
end;


procedure TForm1.N2Click(Sender: TObject);
begin

MediaPlayer1.DisplayRect:=Panel1.ClientRect;

end;


procedure TForm1.Timer1Timer(Sender: TObject);
var TheLength:LongInt;
begin

if MediaPlayer1.FileName<>'' then

CanTimerAutoChange:=True;
TheLength:=MediaPlayer1.Length;
with HMSRec(TheLength)do

begin



Label1.Caption:=IntTostr(Minutes)+':'+IntTostr(Seconds);

end;

end;


end.

 
朋友,不要意思,上面就是我的原码
 
//在打开文件的按钮事件中键入以下代码:
procedure TForm1.Button1Click(Sender: TObject);
var
thelength: integer;
begin

if not Opendialog1.Execute then
Exit;
with Mediaplayer1do

begin

FileName := OpenDialog1.FileName;
Open;
Mediaplayer1.TimeFormat:=tfMilliseconds;
Thelength := Length;
end;

with HMSRec(TheLength)do

begin

Label1.Caption := '文件时间长度:'+IntToStr(thelength div 60000 div 60)+':'+IntToStr(thelength div 60000 mod 60)+':'+IntToStr(thelength div 1000 mod 60);
end;

end;

//我试过了,没问题,但记得给分

 

TheLength:=MediaPlayer1.Length;
之前加上
MediaPlayer1.TimeFormat:=tfHMS;
 
//返回秒数
function ReadWaveTime(FileName: string): Integer;
type
TWaveFileHeader = packed record
FType: Integer;
Size: Longint;
RType: Integer;
end;


TWaveChunkHeader = packed record
CType: Longint;
Size: Longint;
end;


const
ID_RIFF = Ord('R') + Ord('I') * $100 + Ord('F') * $10000 + Ord('F') * $1000000;
ID_WAVE = Ord('W') + Ord('A') * $100 + Ord('V') * $10000 + Ord('E') * $1000000;
ID_FMT = Ord('f') + Ord('m') * $100 + Ord('t') * $10000 + Ord(' ') * $1000000;
ID_FACT = Ord('f') + Ord('a') * $100 + Ord('c') * $10000 + Ord('t') * $1000000;
ID_DATA = Ord('d') + Ord('a') * $100 + Ord('t') * $10000 + Ord('a') * $1000000;

var
DataSize: Integer;
Format: TWaveFormatEx;
Stream: TFileStream;
Header: TWaveFileHeader;
Chunk: TWaveChunkHeader;
begin

Result := 0;
DataSize := 0;
FilLChar(Format, SizeOf(Format), 0);
Stream := TFileStream.Create(FileName, fmOpenRead);
with Streamdo

try
ReadBuffer(Header, Sizeof(Header));
if (Header.FType <> ID_RIFF) and (Header.RType <> ID_WAVE) then

raise Exception.Create('invalid wave file format');
Stream.ReadBuffer(Chunk, Sizeof(Chunk));
while Chunk.CType <> 0do

begin

if Chunk.CType = ID_FMT then

Stream.ReadBuffer(Format, Chunk.Size) else

if Chunk.CType = ID_DATA then

begin

DataSize := Stream.Size - Stream.Position;
break;
end else

if Chunk.CType = ID_FACT then

Stream.Seek(Chunk.Size, soFromCurrent);
FillChar(Chunk, SizeOf(Chunk), 0);
Stream.ReadBuffer(Chunk, Sizeof(Chunk));
end;

if (DataSize <> 0) and (Format.nAvgBytesPerSec <> 0) then

Result := DataSize div Format.nAvgBytesPerSec;
finally
Free;
end;

end;


TWaveFormatEx :
nAvgBytesPerSec: 声音文件中声音频率每秒钟产生出的字节数

 
请问,最后几句,可以解释一下吗?
 
TWaveFormatEx :
nAvgBytesPerSec: 声音文件中声音频率每秒钟产生出的字节数

注释而已
 
朋友,谢谢你哈!如果你能够给我一完整播放器的源码,那就更好了,不知道可以不?
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部