2016-05-05 43 views
1

地獄では、Delphi 10のAndroidバックグラウンドサービスをテストしようとしていますが、サービスが開始されたときに地元のサービスを使ってバックグラウンドで音を録音しようとしています私のコード、Test.cafに聞こえる: のAppDelphi 10を使用してAndroidでバックグラウンドでサウンドを録音する方法

procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    TLocalServiceConnection.StartService('RecService'); 

サービスコード:

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; 

任意のヘルプは、多くのおかげで感謝しています。

答えて

0

どのようなアクセス許可を有効にしましたか? サービスは動作しますか、サウンドファイルが作成されていますか?

  1. スリープ()の代わりにTThreadを開始する必要があります。

  2. モジュールFMX.Media.pas-> FMX.Media.Android.pasはAndroidのアクティビティを使用しており、アンドロイドサービスでは動作しません。そして、サービスは走ってはいけません。 AudioCaptureDeviceを操作するには、別の方法を使用する必要があります。

+0

レコード許可、はい、それは動作しますが、作成していないサウンドファイルを、サービスがまだ実行中に – ColdZer0

1

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. 
関連する問題