2016-04-27 119 views
2

嗨,大家好,我試圖用moveTaskToBack(true);方法將活動發送到後臺後記錄幾秒鐘的音頻。我在記錄開始時創建了一項服務,並設置了我希望記錄音頻的時間。一旦它最小化,問題就來了。它會在2秒鐘或更短時間後停止錄製,我需要錄製大約一分鐘。我想知道如果應用程序在後臺運行,是否真的有可能錄製音頻。在後臺服務中錄製音頻

我的服務記錄是這樣的:

@Override 
    protected void onHandleIntent(Intent intent) { 

    recorder = new MediaRecorder(); 
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
    recorder.setOutputFormat(output_formats[currentFormat]); 
    recorder.setOutputFile(fake.getFilename()); 
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 

    try { 
     recorder.prepare(); 

      recorder.start(); 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        recorder.stop(); 
       } 
      }, 6000000); 

    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

而且我的活動調用服務並關閉這樣的:

public void mecierro() { 
     Intent intent = new Intent(Panic.this, Record_service.class); 
     Panic.this.startService(intent); //starting the service 

     start1 = new CountDownTimer(5000, 1000) { 

      public void onTick(long millisUntilFinished) { 
      } 

      public void onFinish() { 
      moveTaskToBack(true); //"minimizing" the app 
      } 
    }.start(); 


} 

感謝您的幫助!

+0

當你的應用程序轉到後臺時,它可以很容易地被低內存顯示器殺死 - 這是一個如果它可以停止記錄的原因。我會試着抓住一個喚醒鎖 - 你可能不希望設備陷入睡眠模式,而無論如何,請參閱http://developer.android.com/reference/android/os/PowerManager.WakeLock.html –

回答

2

是的,它是百分之百的可能在後臺錄製音頻。 使用服務啓動錄音機。 然後,您啓動此服務的活動使用方法 startForeground()而不是startService() 這是因爲您的服務在它可以進一步記錄之前被殺死。除非用戶強迫前臺服務不太可能被殺。