从Android上的相机记录视频到mp4 [英] Record video from camera on Android to mp4

查看:401
本文介绍了从Android上的相机记录视频到mp4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FireMonkey(Delphi XE6)中似乎有 TVideoCaptureDevice ,但在官方文档中,捕获过程最终会出现:

  if(VideoCamera){
// do something
}

我可以在飞行时将视频录制到mp4吗?尝试在google上查找,但没有找到任何答案...

解决方案

查看以下docwiki的答案的)。



XE7中的Delphi视频捕获

当然,这里的捕获一词意味着,获取视频输入并将其放在显示器上。



以下代码由

的人友善地提供给我



flashavconverter ,并在此批准发布:

 使用
Androidapi.JNI.GraphicsContentViewText;

const
RECORD_VIDEO = 9;

实现

使用
System.IOUtils,
Androidapi.JNI.Provider,
Androidapi.JNI.App,
Androidapi.JNI.Net,
Androidapi.JNIBridge,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Os;

{$ R * .fmx}

procedure TFormMain.btnRecordClick(Sender:TObject);
var
VideoIntent:JIntent;
videoUri:Jnet_Uri;
AFile:JFile;
FileName:TFileName;
begin
FMessageSubscriptionID:=
TMessageManager.DefaultManager.SubscribeToMessage(
TMessageResultNotification,HandleActivityMessage);
VideoIntent:=
TJIntent.JavaClass.init(
TJMediaStore.JavaClass.ACTION_VIDEO_CAPTURE
);
if(
VideoIntent.resolveActivity(
SharedActivityContext.getPackageManager()
)<> nil)then
begin
FileName:= TPath.Combined (
TPath.GetSharedDocumentsPath,'recording.mp4')
ADile:= TJFile.JavaClass.init(
StringToJString(FileName));
videoUri:= TJnet_Uri.JavaClass.fromFile(AFile);
VideoIntent.putExtra(
TJMediaStore.JavaClass.EXTRA_OUTPUT,
TJParcelable.Wrap((videoUri as ILocalObject).GetObjectID));
SharedActivity.startActivityForResult(VideoIntent,RECORD_VIDEO);
end;
end;

procedure TFormMain.HandleActivityMessage(const Sender:TObject;
const M:TMessage);
如果M是TMessageResultNotification然后
OnActivityResult(
TMessageResultNotification(M).RequestCode,
TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M) 。值);
end;

function TFormMain.OnActivityResult(RequestCode,ResultCode:Integer;
Data:JIntent):Boolean;
begin
结果:= False;

TMessageManager.DefaultManager.U unsubscribe(
TMessageResultNotification,FMessageSubscriptionID);
FMessageSubscriptionID:= 0;

if RequestCode = RECORD_VIDEO then
begin
if ResultCode = TJActivity.JavaClass.RESULT_OK then
begin
TThread.Queue(nil,procedure
begin
lable1.Text:='recording completed';
Invalidate;
end);
end;
end;

end;



(附近)完全回答这个问题。启动特定于设备的录像机UI,供用户进行交互。除了保存录制文件的名称之外,没有其他程序控制。作为一个被Android API淹没的Delphi开发人员,我非常感谢这个解决方案。


There seems to be TVideoCaptureDevice in FireMonkey (Delphi XE6), but on official documentation, capturing process ends up on lines:

if(VideoCamera){
  //do something
}

What do I do to record video to mp4 on flight? Tried looking on google, but didn't find any answer...

解决方案

See the following docwiki for an answer (sort-of).

Delphi Video Capturing in XE7

Of course the word "capturing" here means, getting the video input and putting it on the display. "Recording" means joining the frames together to make a movie file.

The following code was kindly provided to me by the people at

flashavconverter and is posted here with their approval:

uses
  Androidapi.JNI.GraphicsContentViewText;

const
  RECORD_VIDEO = 9;

implementation

uses 
  System.IOUtils,
  Androidapi.JNI.Provider,
  Androidapi.JNI.App,
  Androidapi.JNI.Net,
  Androidapi.JNIBridge,
  Androidapi.Helpers,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Os;

{$R *.fmx}

procedure TFormMain.btnRecordClick(Sender: TObject);
var
  VideoIntent: JIntent;
  videoUri: Jnet_Uri;
  AFile: JFile;
  FileName: TFileName;
begin
  FMessageSubscriptionID := 
    TMessageManager.DefaultManager.SubscribeToMessage(
      TMessageResultNotification, HandleActivityMessage);
  VideoIntent := 
    TJIntent.JavaClass.init(
      TJMediaStore.JavaClass.ACTION_VIDEO_CAPTURE
    );
  if (
    VideoIntent.resolveActivity(
      SharedActivityContext.getPackageManager()
    ) <> nil) then
  begin
    FileName := TPath.Combined(
      TPath.GetSharedDocumentsPath, 'recording.mp4')
    AFile:=TJFile.JavaClass.init(
      StringToJString(FileName));
    videoUri:=TJnet_Uri.JavaClass.fromFile(AFile);
    VideoIntent.putExtra(
      TJMediaStore.JavaClass.EXTRA_OUTPUT, 
      TJParcelable.Wrap((videoUri as ILocalObject).GetObjectID));
    SharedActivity.startActivityForResult(VideoIntent, RECORD_VIDEO);
  end;
end;

procedure TFormMain.HandleActivityMessage(const Sender: TObject;
  const M: TMessage);
begin
  if M is TMessageResultNotification then
    OnActivityResult(
      TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
end;

function TFormMain.OnActivityResult(RequestCode, ResultCode: Integer;
  Data: JIntent): Boolean;
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
        lable1.Text:='recording completed';
        Invalidate;
      end);
    end;
  end;

end;

This code is a (near) complete answer to the question. The device-specific video recorder UI is launched for the user to interact with. There is no programmatic control other than the name of the file that the recording is saved to. As a Delphi developer who is overwhelmed by the Android API, I am grateful for this solution.

这篇关于从Android上的相机记录视频到mp4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆