小弟写了个程序,用MediaoPlayer控件循环播放一段视频,在播放过程中的一些特殊时刻要向串口发送数据包,同时要响应键盘按下事件。下面是主要代码,在PlayMedia()中用一个死循环来监控视频播放过程,但这样程序就不能响应键盘事件了;另外我试了一下新建一个线程来执行望PlayMedia()中的代码,但是新线程中MediaPlayer1->Position 的值一直不变,高手指点一下!谢谢了!
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Application->OnMessage = Form1->AppMessage;
}
/********************监控键盘按键消息**********************/
void __fastcall TForm1::AppMessage(tagMSG& msg,bool& Handled)
{
if(msg.message==WM_KEYDOWN){
int key = (int) msg.wParam;
if(key==VK_ESCAPE) //Esc键按下,退出程序
{
MediaPlayer1->Stop() ;
stop=true;
Sleep(1);
this->Close() ;
}
}
}
void __fastcall TForm1:: PlayMedia()
{
FILE *inputfile;
char *p;
int sendtime;
int lastsendtime;
int position;
char temp[10];
sendtime=0;
lastsendtime=0;
inputfile=fopen("D:\\test.txt","r");
Form1->Panel1->Enabled =true;
Form1->Panel1->Visible =true;
Form1->BorderStyle = bsNone;
Form1->WindowState =wsMaximized;
Form1->Panel1-> Left = 0;
Form1->Panel1-> Top = 0;
Form1->Panel1-> Width = Screen->Width ;
Form1->Panel1-> Height = Screen->Height ;
Form1->MediaPlayer1->Open();
Form1->MediaPlayer1->DisplayRect=Rect(0,0,Screen->Width, Screen->Height);
Form1->MediaPlayer1->Play();
while(!stop)
{
/*********读取文件(存放需要向串口发送数据的时刻表)*****/
if(feof(inputfile)) fseek(inputfile,0,SEEK_SET);
else
{
fgets(line,256,inputfile);
p=strtok(line,"\t");
if(p!=NULL) strcpy(crossid,p);
p=strtok(NULL,"\t");
if(p!=NULL) sendtime=atoi(p); //sendtime 要向串口发送数据的时刻
}
position= Form1->MediaPlayer1->Position /20 ;
while((position >=lastsendtime)&&(position <sendtime))
{
Sleep(1000);
position= Form1->MediaPlayer1->Position /20 ;
}
if(Form1->MediaPlayer1->Position < Form1->MediaPlayer1->Length )
{
writeToPort(); //串口发送数据
lastsendtime=sendtime;
}
}
}
/********视频播放完,进行循环播放**********/
void __fastcall TForm1::MediaPlayer1Notify(TObject *Sender)
{
if(MediaPlayer1->Position ==MediaPlayer1->Length )
{
PlayMedia();
}
}
oid __fastcall TForm1::Button1Click(TObject *Sender)
{
stop=false;
// mythread=new MyThread(true);
// mythread->FreeOnTerminate=true;
PlayMedia();
}