• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps MediaRecorder questions.

Delay

Lurker
Hi, I'm having trouble with MediaRecorder. I'm trying to write app that will record audio every 1 minute and save it, but it fails sometimes. For testing purpose, I created files with date format to check if it's working properly. Result showed, that recorded files was delayed with 1 seconds (or less) and after locking phone, it was delaying like 5, 10 or even 20-50 minutes. I'm using it as service, anyone can tell me what the problem is ? Thank you.
Here's the code.

Code:
//Timer to trigger TimerTask
timer = new Timer("myTimer");
timer.scheduleAtFixedRate(updateTask, 0, 60 * 1000L);

Code:
//Service code
public class HelloworldService extends Service {

private static final String TAG = "RecorderService";

//Recorder TimerTask code.
	private TimerTask updateTask = new TimerTask() {
		@Override
		public void run() {
			Log.d("TimerTask","Triggered.");
			recSound("/sdcard/");

		}			
		public void recSound(String path)
		{
			SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
			String fileName = timeStampFormat.format(new Date()) + ".mp4";
			recorder = new MediaRecorder();
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			recorder.setAudioEncodingBitRate(16);
			recorder.setAudioSamplingRate(44100);
			recorder.setOutputFile(path+fileName);
			
					try {
						recorder.prepare();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					recorder.start();
					Log.d("Recorder","Started recording...");
					try
					{
						Thread.sleep(60000);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
					recorder.stop();
					recorder.reset();
					recorder.release();
					Log.d("Recorder","Stop recording...");
		}

		
};
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		Toast.makeText(getApplicationContext(), "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
		timer = new Timer("myTimer");
		timer.scheduleAtFixedRate(updateTask, 0, 60 * 1000L);
	}

	@Override
	public void onDestroy() {
		Toast.makeText(getApplicationContext(), "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onStart(Intent intent, int startid) {
		Toast.makeText(getApplicationContext(), "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");
		
	}
}

Thank you.
 
Back
Top Bottom