wav文件的时间长度(100分)

  • 主题发起人 microbit
  • 开始时间
M

microbit

Unregistered / Unconfirmed
GUEST, unregistred user!
有什么样的方法精确确定wav文件的播放时间长度?
 
你可以使用一个TIMER控件,把它的Interval设置为1,也就是一毫秒,再用一个
MediaPlayer控件来播放这个WAV文件,使用一个全局变量在onTimer事件来累加,
播完了后,ShowMessage这个全局变量的值。
 
还有什么好方法?
 
嗯, 有, 就是计算wav的时间长度啊, 每个wav文件都有一个头部, 在这个头部里
包含了声音的质量, 即采样频率, 如44100Hz啦, 而每次取样的位数也是在这个头部
里记录, 例如16位(2字节), 然后是单声道还是双声道

例如CD音质的wav, 它一秒钟的长度就是44100*2(16位)*2(双声道)=176400字节

你只要理解它的存储结构, 可以计算出精确的时间. Wav文件的头部数据结构在很多
有关wav的构件代码中都有定义.
 
用MediaPlayer控件打开,读Length属性
 
//返回秒数
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: 声音文件中声音频率每秒钟产生出的字节数

 
delphibyte go on please!
 
wav默认的播放模式是以毫秒播放的
取得MEDIAPLAYER.LENGTH/1000即可单位是秒
 
buttonclick1()
begin

mediaplayer1.filename:='c:/12.wav';
mediaplayer1.open;
mediaplayer1.play;
button1.caption:=timetostr(time());
end;

procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin

button1.caption:=timetostr(time());
end;

两个时间相减
或者
用mediaplayer打开wav文件
时间长度:mediaplayer1.length 单位/毫秒


 

Similar threads

回复
0
查看
855
不得闲
回复
0
查看
475
不得闲
回复
0
查看
575
不得闲
回复
0
查看
651
不得闲
顶部