[英]How to record sound in the background in android using Delphi 10
Hell, I am trying to test Delphi 10 Android background services, i am trying to record sound in the background using local service,however, when the service starts its dosent recored sound to Test.caf
, my codes : the App地狱,我正在尝试测试 Delphi 10 Android 后台服务,我正在尝试使用本地服务在后台录制声音,但是,当该服务开始向
Test.caf
声音时,我的代码:应用程序
procedure TForm1.Button1Click(Sender: TObject);
begin
TLocalServiceConnection.StartService('RecService');
The service code :服务代码:
function TAndroidServiceDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Result := TJService.JavaClass.START_STICKY;
FMicrophone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
{ and attempt to record to 'test.caf' file }
FMicrophone.FileName := '/sdcard/1/test.caf';
FMicrophone.StartCapture;
sleep(5000);
FMicrophone.StopCapture;
any help appreciated many thanks.任何帮助表示感谢,非常感谢。
Android Services in RAD Studio don't work with FMX... modules. RAD Studio 中的 Android 服务不适用于 FMX... 模块。 To work with media need to use low level modules.
使用媒体需要使用低级模块。
I realized the issue that method (hope it will usefull):我意识到该方法的问题(希望它有用):
uses
...
AndroidApi.JNI.Media, // JMediaRecorder
AndroidApi.Timer, // Timer
...;
Const
TimerInterval = 1000;
TimerCounterSecLimit = 10;
type
TDM = class(TAndroidService)
...
private
FTimerHandle: Integer;
FRecording: Boolean;
procedure StartRecord;
procedure StopRecord;
procedure StartTimer;
procedure StopTimer;
public
FAudioRec: JMediaRecorder;
end;
procedure TDM.AndroidServiceCreate(Sender: TObject);
begin
FTimerHandle := 0;
FTimerCounter := 0;
FRecording := false;
end;
procedure TDM.AndroidServiceDestroy(Sender: TObject);
begin
StopTimer;
StopRecord;
end;
function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
if Intent.getAction.equalsIgnoreCase(StringToJString('StopIntent')) then
begin
StopTimer;
StopRecord;
Result := TJService.JavaClass.START_NOT_STICKY; // don't reload service
Log('- service stoped', []);
end
else begin
if not FRecording then
begin
Log('... sound record to be started', []);
StartRecord;
StartTimer;
end;
Result := TJService.JavaClass.START_STICKY; // rerun service if it stops
Log('+ Service started', []);
end;
end;
procedure TDM.StartRecord;
begin
StopRecord;
FAudioRec := TJMediaRecorder.Create;
FAudioRec.setAudioSource(TJMediaRecorder_AudioSource.JavaClass.MIC);
FAudioRec.setOutputFormat(TJMediaRecorder_OutputFormat.JavaClass.THREE_GPP);
FAudioRec.setAudioEncoder(TJMediaRecorder_AudioEncoder.JavaClass.AMR_NB);
FAudioRec.setOutputFile(StringToJString(TPath.Combine(TPath.GetSharedMusicPath, 'myrecord.3gp')));
try
FAudioRec.Prepare();
FAudioRec.start;
FRecording := True;
Log('+ Start record to %s', [TPath.Combine(TPath.GetSharedMusicPath, 'myrecord.3gp')]);
except
on E: Exception do
Log('- Error in mic recording: %s', [E.Message]);
end;
end;
procedure TDM.StopRecord;
begin
if Assigned(FAudioRec) then
begin
if FRecording then
begin
FRecording := false;
try
FAudioRec.stop();
FAudioRec.release();
Log('- Mic recording is stoped');
except
on E: Exception do
Log('- Error in mic stop recording: %s', [E.Message]);
end;
end;
end
else
begin
FRecording := false;
end;
end;
procedure TDM.WaitComplete(TimerId: Integer);
begin
if FTimerCounter < TimerCounterSecLimit then
begin
Log('+++ Timer is triggered %d time.', [FTimerCounter]);
inc(FTimerCounter);
end
else
StopTimer;
end;
procedure TDM.StartTimer;
begin
FTimerCounter := 0;
if FTimerHandle = 0 then
begin
FTimerHandle := AndroidTimerCreate;
AndroidTimerSetInterval(FTimerHandle, TimerInterval);
end;
AndroidTimerSetHandler(WaitComplete);
Log('+ Timer started', []);
end;
procedure TDM.StopTimer;
begin
if FTimerHandle > 0 then
begin
Log('... MIC recording to be stopped');
StopRecord;
AndroidTimerSetHandler(nil);
Log('- Timer stoped', []);
end;
end;
end.
What kind of permissions you enabled?你启用了什么样的权限? Does the service work, sound file created?
服务是否有效,是否创建了声音文件?
It needs to start а TThread instead of Sleep().它需要启动一个 TThread 而不是 Sleep()。
Module FMX.Media.pas->FMX.Media.Android.pas use Android activities, and it doesn't work in android services.模块 FMX.Media.pas->FMX.Media.Android.pas 使用 Android Activity,在 android 服务中不起作用。 And service should not run.
并且服务不应该运行。 It needs to use alternate methods to work with AudioCaptureDevice.
它需要使用替代方法来使用 AudioCaptureDevice。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.