// // 视频录制回调... function TForm14.OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean; var FS: TFileStream; begin Result := False; TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID); FMessageSubscriptionID := 0; if RequestCode = RECORD_VIDEO then begin if ResultCode = TJActivity.JavaClass.RESULT_OK then begin TThread.Queue(nil, procedure begin if (TFile.Exists(VFileName)) then begin try FS := TFileStream.Create(VFileName, fmShareDenyNone); memo1.Lines.Add('视频录制成功(myvideo.mp4)!请播放测试!'); finally fs.Free; end; end; Invalidate; end); end; end; end;
// // 订阅的消息... procedure TForm14.HandleActivityMessage(const Sender: TObject; const M: TMessage); begin if M is TMessageResultNotification then begin OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode, TMessageResultNotification(M).Value); end; end;
// // 连接服务器... procedure TForm14.SpeedButton1Click(Sender: TObject); begin if MBConn.Connect then begin memo1.Lines.Add('连接服务器成功!'); Receiver.Active:=true; memo1.Lines.Add('消息接收器开启!'); RemoteFileName:='uploads\myvideo.mp4'; QMFFileName:='homepage\video\myvideo.qmf'; localplaying:=false; remoteplaying:=false; end else memo1.Lines.Add('*** 连接服务器失败,Error='+MBConn.LastError); end;
// // 录制视频... procedure TForm14.Button1Click(Sender: TObject); var VideoIntent: JIntent; videoUri: Jnet_Uri; AFile: JFile; begin FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage); VideoIntent := TJIntent.JavaClass.init(TJMediaStore.JavaClass.ACTION_VIDEO_CAPTURE); if (VideoIntent.resolveActivity(TAndroidHelper.Context.getPackageManager()) <> nil) then begin VFileName := TPath.Combine(TPath.GetSharedDocumentsPath, 'myvideo.mp4'); AFile := TJFile.JavaClass.init(StringToJString(VFileName)); videoUri := TJnet_Uri.JavaClass.fromFile(AFile); VideoIntent.putExtra(TJMediaStore.JavaClass.EXTRA_OUTPUT, TJParcelable.Wrap((videoUri as ILocalObject).GetObjectID)); TAndroidHelper.Activity.startActivityForResult(VideoIntent, RECORD_VIDEO); end; end;
// // 本地MP4播放... procedure TForm14.Button6Click(Sender: TObject); begin if localplaying then begin browser.Visible:=false; memopanel.Visible:=true; Button6.Text:='MP4视频播放'; localplaying:=false; end else begin memopanel.Visible:=false; browser.Visible:=true; browser.Navigate('file://'+vfilename); Button6.Text:='结束播放'; localplaying:=true; end; end;
// // 上传MP4到服务端... procedure TForm14.Button7Click(Sender: TObject); var FirstBlockId,i: integer; ok: boolean; begin browser.Visible:=false; memopanel.Visible:=true; progresspanel.Visible:=true; FirstBlockId:=0; for i := 1 to 5 do begin ok:=ftp.UploadFile(VFileName,RemoteFileName,FirstBlockId); if ok then break; FirstBlockId:=ftp.LastOkBlockId+1; memo1.Lines.Add('*** 上传MP4文件到服务端失败,开始重试...'); application.ProcessMessages; end; if ok then begin memo1.Lines.Add('上传MP4文件到服务端成功,请继续切片处理!'); progresspanel.Visible:=false; end else memo1.Lines.Add('*** 尝试多次上传MP4文件到服务端仍失败,无法继续测试.'); end;
// // 开始HLS切片及打包... procedure TForm14.Button2Click(Sender: TObject); var Params: TConvertParameters; begin browser.Visible:=false; memopanel.Visible:=true; // // 转换参数... Params.Height:=round(memo1.Height); Params.Width:=round(memo1.Width); Params.PieceSeconds:=6; Params.FrameRate:=25; Params.VideoBitrate:=1000; Params.AudioBitrate:=80; Params.CodeLength:=4; // // 开始调用... if Files.MediaConvertToQMF(RemoteFileName,qmffilename,Params) then memo1.Lines.Add('提交服务端切片打包任务成功,请等待处理结果...') else memo1.Lines.Add('*** 提交服务端切片打包任务失败!Error='+Files.LastError); end;
// // HLS切片及打包回调(收到消息时)... procedure TForm14.ReceiverMessageArrives(Sender: TObject; NewMsgCount: Integer); var MsgParcel: TMBParcel; begin browser.Visible:=false; memopanel.Visible:=true; Receiver.PopMessage(MsgParcel); if MsgParcel.GetStringGoods('MsgType')<>'AsyncExecuteResult' then exit; if MsgParcel.GetBooleanGoods('ProcessResult') then memo1.Lines.Add('切片打包任务成功完成!') else memo1.Lines.Add('*** 切片打包任务执行失败!'); end;
// // 取QMF文件属性... procedure TForm14.Button3Click(Sender: TObject); var Prop: TQMFProperty; begin browser.Visible:=false; memopanel.Visible:=true; if Files.GetRemoteQMFProperty(qmffilename,prop) then begin memo1.Lines.Add('获取服务端QMF文件属性成功!'); memo1.Lines.Add('切片数据块数:'+inttostr(prop.BlockCount)); memo1.Lines.Add('画面宽度:'+inttostr(prop.Width)); memo1.Lines.Add('画面高度:'+inttostr(prop.Height)); memo1.Lines.Add('切片时长(秒):'+inttostr(prop.PieceSeconds)); memo1.Lines.Add('帧率(帧/秒):'+inttostr(prop.FrameRate)); memo1.Lines.Add('视频比特率:'+inttostr(prop.VideoBitrate)+'k'); memo1.Lines.Add('音频比特率:'+inttostr(prop.AudioBitrate)+'k'); memo1.Lines.Add('切片文件名长度:'+inttostr(prop.NameLength)); memo1.Lines.Add('切片编码长度:'+inttostr(prop.CodeLength)); end else memo1.Lines.Add('*** 获取服务端QMF文件属性失败!'); end;
// // 远程播放测试QMF... procedure TForm14.Button4Click(Sender: TObject); var Url: string; begin if remoteplaying then begin browser.Visible:=false; memopanel.Visible:=true; Button4.Text:='远程播放QMF'; remoteplaying:=false; end else begin url:='http://'+MBConn.Host; if MBConn.Port<>80 then url:=url+':'+inttostr(MBConn.Port); url:=url+'/video/myvideo/index.m3u8'; memopanel.Visible:=false; browser.Visible:=true; browser.Navigate(url); remoteplaying:=true; Button4.Text:='结束播放'; end; end;