使用DELPHI实现文本文件显示和声音同 步

2016-02-19 17:27 24 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的使用DELPHI实现文本文件显示和声音同 步懂设计的网友们快点来了解吧!

【 tulaoshi.com - 编程语言 】

  在一些语言教学软件中,经常使用文本显示和声音同步播放,我们可以用DEL PHI实现之。

  一、材料的制作

  首先,我们选择几篇文章,这里我们选择马丁路德金、林肯和里根的演说,每一篇文章分为若干句,在制作声音文件时,记下每个句子的开始时间和结束时间,然后将其记到一个数据表中,这个表有如下字段:NO、TEXT、STARTTIME,分别表示每句序号、内容、开始时间。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  二、 界面的制作

  在DELPHI中创建一个新工程,在FORM中放入多媒体控件,RTF编辑器,命令按钮,单选分组框,数据表,时钟和标签,如图所示

  三、各个控件的属性

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  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);beginTable1.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);beginMediaPlayer1.Close; Table1.Close;case radiogroup1.ItemIndex of0: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);beginDoDefault:=False;with MediaPlayer1 dobegincase Button ofbtPlay : beginif isend=true thenbeginTable1.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 thenCurrentButton:=btpauseelse if CurrentButton=btpause thenCurrentButton:=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 ofbtPlay: Play;btpause:pause;btStop:Stop;end;end;end;  
   5、为了能同步显示文本,需要编写定时器OnTime事件的代码,如果当前播放的时间超过了当前记录的开始时间,则设置CurrentPlay为当前记录号,如果CurrentPlay超过了CurrentDisp ,则显示当前记录。
procedure TForm1.Timer1Timer(Sender: TObject);beginwith MediaPlayer1 dobeginif CurrentButton=btPlay thenbeginif not Table1.eof and (Position  $#@62;Table1.FieldValues[STARTTIME])thenbegin CurrentPlay:=Table1.recno;if CurrentPlay $#@62;=CurrentDisp thenbeginRichEdit1.Lines.add(Table1.fieldvalues[TEXT]);CurrentDisp:=CurrentDisp+1;end;Table1.Next;end;if Table1.eof thenbeginCurrentButton:=btStop;isend:=true;end;end;end;end;
   注:在MediaPlayer的click事件中,使用了btPrev,btNext等常量,为了能够通过编译,在uses子句中需将ComCtrls放在mPlayer之前,以免引起冲突。

来源:https://www.tulaoshi.com/n/20160219/1615250.html

延伸阅读
标签: ASP
  try { // create a new ADOConnection to the text file through ODBC and an existing Data Source ADOConnection conn = new ADOConnection("Provider=MSDASQL;DSN=registrations;"); // create a DataSet Command that selects all the records from the registration.txt table (which in this case is a file) ADOD...
System.IO命名空间中的类为托管应用程序提供文件以及其他形式的输入输出。托管i/o的基本构件是流,而流是字节导向的数据的抽象表示。流通过System.IO.Stream类表示. System.IO.FileStream允许将文件作为流访问; System.IO.MemoryStream允许将内存块作为流进行访问; 托管和非托管的应用程序最常使用的IO形式是文件IO。托管应用程序读写文件的一...
标签: 办公软件
在工作中,有可能需要将大量Word的DOC文档格式转换为TXT文本文件格式。如果转换量少,可以在Word中利用“另存为”命令,但是当转换量比较大的时候用“另存为”的方法就很麻烦也很慢了,我们下面介绍如何在Word中利用“转换向导”快速完成这个任务的方法。具体操作以Word2003为例,其它版本可以参照进行。 1. 为了便于管理和操作,我们先...
/*    * 简单的读/写文本文件的示例    * 这里包含了三个例子,即    * 1. 将文件读入到内存(这里是StringBuffer)的例子    * 2. 将内容中的文本写到文件    * 3. 将一个文件的内容读出来写入另一个文件中 &n...
标签: PHP
  动态实时发布新闻的程序,可以根据发布时间,自动地排列显示顺序。新闻文件为存储在指定目录的文本文件组成,当有新闻的时候,程序会自动地将其显示出来,并且排列在页面的最开始部分。 <html <head <titleNews</title </head <body bfcolor="#ffffff" <h1News</h1 <div aligh="cente...

经验教程

691

收藏

19
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部