語言學習軟體:文章分句錄製波形檔 (.wav),資料庫記錄每句起始時間 (毫秒)。播放時依據目前播放位置自動捲動高亮顯示對應句子。
Table: Sentences
- No: Integer (序號)
- Text: String (句子內容)
- StartTime: Integer (毫秒起始時間)
TMediaPlayer (MediaPlayer1):播放 .wav,TimeFormat := tfMilliseconds。TRichEdit (RichEdit1):顯示文章,逐句加入。TRadioGroup (RadioGroup1):選擇文章 (對應不同 .wav/.dbf)。TTable (Table1):連接句子資料表。TTimer (Timer1):間隔 ~100ms 檢查同步。
var
IsEnd: Boolean;
CurrentButton: TMPBtnType; // 記錄目前播放器按鈕狀態
CurrentPlay, CurrentDisp: Longint; // 目前播放/顯示句號
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.TableName := 'ex1.dbf'; Table1.Open;
MediaPlayer1.FileName := 'ex1.wav'; MediaPlayer1.Open;
MediaPlayer1.TimeFormat := tfMilliseconds;
IsEnd := False; CurrentButton := btStop;
CurrentPlay := 1; CurrentDisp := 1;
RichEdit1.Lines.Clear; RichEdit1.Lines.Add('演講內容');
end;
procedure TForm1.RadioGroup1Click(Sender: TObject);
begin
MediaPlayer1.Close; Table1.Close;
case RadioGroup1.ItemIndex of
0: begin MediaPlayer1.FileName := 'ex1.wav'; Table1.TableName := 'ex1.dbf'; end;
1: begin MediaPlayer1.FileName := 'ex2.wav'; Table1.TableName := 'ex2.dbf'; end;
2: begin MediaPlayer1.FileName := 'ex3.wav'; Table1.TableName := 'ex3.dbf'; end;
end;
Table1.Open; MediaPlayer1.Open;
end;
procedure TForm1.MediaPlayer1Click(Sender: TObject; Button: TMPBtnType; var DoDefault: Boolean);
begin
DoDefault := False;
with MediaPlayer1 do
begin
case Button of
btPlay:
if IsEnd then begin Table1.First; Position := 0; CurrentPlay := 1; CurrentDisp := 1; IsEnd := False; RichEdit1.Lines.Clear; RichEdit1.Lines.Add('演講內容'); end;
btStop: IsEnd := True;
btPause: if CurrentButton = btPlay then CurrentButton := btPause else if CurrentButton = btPause then CurrentButton := btPlay;
btPrev: begin Table1.Prior; Position := Table1.FieldByName('StartTime').AsInteger; end;
btNext: begin Table1.Next; Position := Table1.FieldByName('StartTime').AsInteger; end;
btBack: begin Table1.First; Position := 0; CurrentPlay := 1; CurrentDisp := 1; RichEdit1.Lines.Clear; RichEdit1.Lines.Add('演講內容'); end;
end;
case CurrentButton of btPlay: Play; btPause: Pause; btStop: Stop; end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with MediaPlayer1 do
if CurrentButton = btPlay then
begin
if not Table1.Eof and (Position >= Table1.FieldByName('StartTime').AsInteger) then
begin
CurrentPlay := Table1.RecNo;
if CurrentPlay >= CurrentDisp then
begin
RichEdit1.Lines.Add(Table1.FieldByName('Text').AsString);
Inc(CurrentDisp);
end;
Table1.Next;
end;
if Table1.Eof then begin CurrentButton := btStop; IsEnd := True; end;
end;
end;
uses 子句中 ComCtrls 必須置於 MPlayer 之前,避免 TMPBtnType 衝突。TimeFormat := tfMilliseconds 才精確。StartTime 索引加速 Locate/導航。