AndroidスタジオのサービスはFMX ...モジュールでは動作しません。メディアで作業するには、低レベルのモジュールを使用する必要があります。
私は方法は(それは便利願っています)という問題が実現!:ユーザーによって終了されるまで
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.
レコード許可、はい、それは動作しますが、作成していないサウンドファイルを、サービスがまだ実行中に – ColdZer0