如何通过Firemonkey设备录制视频和音频?

时间:2015-12-22 12:46:52

标签: delphi firemonkey

我的视频录制代码,录制不顺畅,即我转动相机的地方最晚出现在预览视图上。我如何解决这个问题

单位VideoAttachmentUnit;

            interface

            uses
            System.SysUtils,
            System.Types,
            System.UITypes,
            System.Classes,
            System.Variants,
            FMX.Types,
            FMX.Controls,
            FMX.Forms,
            FMX.Dialogs,
            FMX.StdCtrls,
            FMX.Media,
            FMX.Platform,
            FMX.Objects,
            FMX.Layouts,
            FMX.Memo,
            FMX.Controls.Presentation;

            type
            TVideoAttachmentForm = class(TForm)
            NavBar: TToolBar;
            CameraChangeBtn: TButton;
            PlayBtn: TButton;
            CloseScreenBtn: TButton;
            ToolBar1: TToolBar;
            StartRecordingBtn: TButton;
            StopRecordingBtn: TButton;
            ImageCameraView: TImage;
            CameraComponent: TCameraComponent;

            procedure FormCreate(Sender: TObject);
            procedure CloseScreenBtnClick(Sender: TObject);
            procedure FormShow(Sender: TObject);
            procedure CameraChangeBtnClick(Sender: TObject);
            procedure StartRecordingBtnClick(Sender: TObject);
            procedure StopRecordingBtnClick(Sender: TObject);
            procedure CameraComponentSampleBufferReady(Sender: TObject;
            const ATime: TMediaTime);
            private
            { Private declarations }
            procedure GetImage;
            procedure InitialSettingsForTheRecording;

            public
            function AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
            end;

            var
            VideoAttachmentForm: TVideoAttachmentForm;
            WhichCamera:String;


            procedure DisplayTheVideoAttachmentScreen;

            implementation

            {$R *.fmx}
            procedure DisplayTheVideoAttachmentScreen;
            begin
            try
            Application.CreateForm(TVideoAttachmentForm , VideoAttachmentForm);
            VideoAttachmentForm.Show;
            finally
            end;
            end;

            procedure TVideoAttachmentForm.CameraChangeBtnClick(Sender: TObject);
            var
            LActive: Boolean;
            begin
            { Select Back Camera }
            LActive := CameraComponent.Active;
            try
            CameraComponent.Active := False;
            if WhichCamera = 'BackCamera' then
            begin
            CameraComponent.Kind := TCameraKind.FrontCamera;
            WhichCamera          := 'FrontCamera';
            end
            else if WhichCamera = 'FrontCamera' then
            begin
            CameraComponent.Kind := TCameraKind.BackCamera;
            WhichCamera          := 'BackCamera';
            end;
            finally
            CameraComponent.Active := LActive;
            end;

            end;

            procedure TVideoAttachmentForm.CameraComponentSampleBufferReady(Sender: TObject;
            const ATime: TMediaTime);
            begin
            TThread.Synchronize(TThread.CurrentThread, GetImage);
            ImageCameraView.Width := ImageCameraView.Bitmap.Width;
            ImageCameraView.Height := ImageCameraView.Bitmap.Height;
            end;

            procedure TVideoAttachmentForm.CloseScreenBtnClick(Sender: TObject);
            begin
            VideoAttachmentForm.Close;
            end;

            procedure TVideoAttachmentForm.FormCreate(Sender: TObject);
            var
            AppEventSvc: IFMXApplicationEventService;
            begin
            if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(AppEventSvc)) then
            AppEventSvc.SetApplicationEventHandler(AppEvent);
            end;

            procedure TVideoAttachmentForm.FormShow(Sender: TObject);
            begin
            InitialSettingsForTheRecording;
            end;

            function TVideoAttachmentForm.AppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
            begin
            case AAppEvent of
            TApplicationEvent.WillBecomeInactive:
            CameraComponent.Active := False;
            TApplicationEvent.EnteredBackground:
            CameraComponent.Active := False;
            TApplicationEvent.WillTerminate:
            CameraComponent.Active := False;
            end;
            end;

            procedure  TVideoAttachmentForm.InitialSettingsForTheRecording;
            var
            LSettings: TVideoCaptureSetting;
            begin
            CameraComponent.Kind := TCameraKind.BackCamera;
            WhichCamera           := 'BackCamera';
            if CameraComponent.HasTorch then
            begin
            CameraComponent.TorchMode := TTorchMode.ModeAuto;
            end;
            CameraComponent.Quality   := TVideoCaptureQuality.CaptureSettings;
            CameraComponent.CaptureSettingPriority := TVideoCaptureSettingPriority.FrameRate;
            end;

            procedure TVideoAttachmentForm.StartRecordingBtnClick(Sender: TObject);
            begin
            CameraComponent.Active := True;
            end;

            procedure TVideoAttachmentForm.StopRecordingBtnClick(Sender: TObject);
            begin
            CameraComponent.Active := False;
            end;

            procedure TVideoAttachmentForm.GetImage;
            begin
            CameraComponent.SampleBufferToBitmap(ImageCameraView.Bitmap, True);
            end;

            end.

0 个答案:

没有答案