首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 多媒體教學軟體:文字顯示與聲音同步播放實作

     編輯:Delphi

應用場景

語言學習軟體:文章分句錄製波形檔 (.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 衝突。
  • 波形檔需為 PCM 格式,TimeFormat := tfMilliseconds 才精確。
  • 資料庫建議建立 StartTime 索引加速 Locate/導航。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved