|
朱群生
---- 在一些語(yǔ)言教學(xué)軟件中,經(jīng)常使用文本顯示和聲音同步播放,我們可以用DEL PHI實(shí)現(xiàn)之。
---- 一、材料的制作
---- 首先,我們選擇幾篇文章,這里我們選擇馬丁路德金、林肯和里根的演說,每一篇文章分為若干句,在制作聲音文件時(shí),記下每個(gè)句子的開始時(shí)間和結(jié)束時(shí)間,然后將其記到一個(gè)數(shù)據(jù)表中,這個(gè)表有如下字段:NO、TEXT、STARTTIME,分別表示每句序號(hào)、內(nèi)容、開始時(shí)間。
---- 二、 界面的制作
---- 在DELPHI中創(chuàng)建一個(gè)新工程,在FORM中放入多媒體控件,RTF編輯器,命令按鈕,單選分組框,數(shù)據(jù)表,時(shí)鐘和標(biāo)簽,如圖所示
---- 三、各個(gè)控件的屬性
---- 1、 多媒體控件的AutoEnable和AutoOpen特性設(shè)置為False ,VisibleButtons特性設(shè)置Record、Eject、Step為不可見。
---- 2、 Table控件的DatabaseName設(shè)為存放表的目錄,我們將應(yīng)用程序、聲音文件和表存放在myprog目錄,因此這里將DatabaseName設(shè)為c:\myprog,將TableName設(shè)為默認(rèn)的播放文件對(duì)應(yīng)的數(shù)據(jù)表的名字,這里設(shè)為ex1.dbf。
---- 3、 Radiogroup控件的Caption設(shè)為 ‘請(qǐng)選擇播放內(nèi)容’,Itmes特性中加上三行:馬丁路德金,林肯,里根。
---- 4、 Richedit控件的Lines特性加上‘演講內(nèi)容。
---- 四、代碼的編寫
---- 1、變量聲明
var isend:Boolean; CurrentButton:TMPBtnType; CurrentPlay,CurrentDisp:longint;
---- 其中isend表示播放是否已經(jīng)到了末尾,CurrentButton表示當(dāng)前MediaPlayer元件中按下了哪個(gè)按鈕,CurrentPlay ,CurrentDisp表示當(dāng)前播放記錄及當(dāng)前顯示記錄。 ---- 2、在FormCreate事件中做一些必要的準(zhǔn)備工作,其代碼如下:
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、在單選分組框中加進(jìn)如下代碼: 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;
---- 4、為了適當(dāng)修改MediaPlayer元件中各個(gè)按鈕的功能,需要編寫MediaPlayer元件的click事件的代碼,主要是修改“快進(jìn)”及“快退”的功能,使其每次移動(dòng)一句,且移動(dòng)后直接播放,而不用再按“播放”按鈕。為達(dá)到這個(gè)功能,在過程的一開始,設(shè)置DoDefault參數(shù)為False,表示不執(zhí)行默認(rèn)的動(dòng)作,在過程的中間部分,加上必要的處理,在過程的結(jié)束處,寫上根據(jù)按下的按鈕執(zhí)行相應(yīng)功能語(yǔ)句。 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 ('演講內(nèi)容'); 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 ('演講內(nèi)容'); 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、為了能同步顯示文本,需要編寫定時(shí)器OnTime事件的代碼,如果當(dāng)前播放的時(shí)間超過了當(dāng)前記錄的開始時(shí)間,則設(shè)置CurrentPlay為當(dāng)前記錄號(hào),如果CurrentPlay超過了CurrentDisp ,則顯示當(dāng)前記錄。 procedure TForm1.Timer1Timer(Sender: TObject); begin with MediaPlayer1 do begin if CurrentButton=btPlay then begin if not Table1.eof and (Position >Table1.FieldValues['STARTTIME']) then begin CurrentPlay:=Table1.recno; if CurrentPlay >=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之前,以免引起沖突。
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!