程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 使用DELPHI實現文本文件顯示和聲音同步

使用DELPHI實現文本文件顯示和聲音同步

編輯:Delphi

在一些語言教學軟件中,經常使用文本顯示和聲音同步播放,我們可以用DELPHI實現之。

一、材料的制作

首先,我們選擇幾篇文章,這裡我們選擇馬丁路德金、林肯和裡根的演說,每一篇文章分為若干句,在制作聲音文件時,記下每個句子的開始時間和結束時間,然後將其記到一個數據表中,這個表有如下字段:NO、TEXT、STARTTIME,分別表示每句序號、內容、開始時間。

二、界面的制作

在DELPHI中創建一個新工程,在FORM中放入多媒體控件,RTF編輯器,命令按鈕,單選分組框,數據表,時鐘和標簽,如圖所示

三、各個控件的屬性

1、多媒體控件的AutoEnable和AutoOpen特性設置為False ,VisibleButtons特性設置Record、Eject、Step為不可見。

2、Table控件的DatabaseName設為存放表的目錄,我們將應用程序、聲音文件和表存放在myprog目錄,因此這裡將DatabaseName設為c:\myprog,將TableName設為默認的播放文件對應的數據表的名字,這裡設為ex1.dbf。

3、Radiogroup控件的Caption設為 ‘請選擇播放內容’,Itmes特性中加上三行:馬丁路德金,林肯,裡根。

4、Richedit控件的Lines特性加上‘演講內容。

四、代碼的編寫

1、變量聲明

var isend:Boolean;
CurrentButton:TMPBtnType;
CurrentPlay,CurrentDisp:longint;

其中isend表示播放是否已經到了末尾,CurrentButton表示當前MediaPlayer元件中按下了哪個按鈕,CurrentPlay ,CurrentDisp表示當前播放記錄及當前顯示記錄。

2、在FormCreate事件中做一些必要的准備工作,其代碼如下:

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;
CurrentDisp:=1;
CurrentPlay:=1;
end;

3、在單選分組框中加進如下代碼:

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:=ex  av;
Table1.TableName:=ex3.dbf; end;
end;
Table1.Open; MediaPlayer1.Open;
end;

4、為了適當修改MediaPlayer元件中各個按鈕的功能,需要編寫MediaPlayer元件的click事件的代碼,主要是修改“快進”及“快退”的功能,使其每次移動一句,且移動後直接播放,而不用再按“播放”按鈕。為達到這個功能,在過程的一開始,設置DoDefault參數為False,表示不執行默認的動作,在過程的中間部分,加上必要的處理,在過程的結束處,寫上根據按下的按鈕執行相應功能語句。 procedure TForm1.MediaPlayer1Click(Sender: TObject;
Button: TMPBtnType; var DoDefault: Boolean);
begin
DoDefault:=False;
with MediaPlayer1 do
begin
case Button of
btPlay : begin
if isend=true then
begin
Table1.first; Position:=start;
CurrentPlay:=1; CurrentDisp:=1;
isend:=False;
RichEdit1.lines.clear; RichEdit1.lines.add
(演講內容);
end;
CurrentButton:=btPlay;
end;
btStop : begin CurrentButton:=btStop;
isend:=true;
end;
btpause: if CurrentButton=btPlay then
CurrentButton:=btpause
else if CurrentButton=btpause then
CurrentButton:=btPlay;
btPrev: begin CurrentButton:=btPrev;
Table1.Prior;
Position:=Table1.fieldvalues
[STARTTIME];
CurrentButton:=btPlay;
end;
btBack: begin CurrentButton:=btBack;
Table1.first; Position:=start;
CurrentPlay:=1;
CurrentDisp:=1;
RichEdit1.lines.clear;
RichEdit1.lines.add
(演講內容);
CurrentButton:=btPlay;
end;
btNext: begin CurrentButton:=btNext;
Table1.Next;
Position:=Table1.fieldvalues
[STARTTIME];
CurrentButton:=btPlay;
end;
end;
case CurrentButton of
btPlay: Play;
btpause:pause;
btStop:Stop;
end;
end;
end;

5、為了能同步顯示文本,需要編寫定時器OnTime事件的代碼,如果當前播放的時間超過了當前記錄的開始時間,則設置CurrentPlay為當前記錄號,如果CurrentPlay超過了CurrentDisp ,則顯示當前記錄。

procedure TForm1.Timer1Timer(Sender: TObject);
begin
with MediaPlayer1 do
begin
if CurrentButton=btPlay then
begin
if not Table1.eof and (Position
$#@62;Table1.FieldValues[STARTTIME])
then
begin CurrentPlay:=Table1.recno;
if CurrentPlay $#@62;=CurrentDisp then
begin
RichEdit1.Lines.add(Table1.fieldvalues[TEXT]);
CurrentDisp:=CurrentDisp+1;
end;
Table1.Next;
end;
if Table1.eof then
begin
CurrentButton:=btStop;
isend:=true;
end;
end;
end;
end;

注:在MediaPlayer的click事件中,使用了btPrev,btNext等常量,為了能夠通過編譯,在uses子句中需將ComCtrls放在mPlayer之前,以免引起沖突。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved