From e53824306292f69f2fc354faa92a6d02c78868fa Mon Sep 17 00:00:00 2001 From: DrKLO Date: Sun, 23 Mar 2014 04:09:24 +0400 Subject: [PATCH] remove main thread block before recording audio --- .../telegram/messenger/MediaController.java | 47 +++++++++++-------- .../java/org/telegram/ui/ChatActivity.java | 16 ++++++- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java index a55cd5d08..3b3cb8cad 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MediaController.java @@ -75,6 +75,8 @@ public class MediaController implements NotificationCenter.NotificationCenterDel public final static int audioProgressDidChanged = 50001; public final static int audioDidReset = 50002; public final static int recordProgressChanged = 50003; + public final static int recordStarted = 50004; + public final static int recordStartError = 50005; private HashMap>> loadingFileObservers = new HashMap>>(); private HashMap observersByTag = new HashMap(); @@ -782,10 +784,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel return isPaused; } - public boolean startRecording(final long dialog_id) { - final Semaphore semaphore = new Semaphore(0); - final Boolean[] result = new Boolean[1]; - + public void startRecording(final long dialog_id) { try { Vibrator v = (Vibrator) ApplicationLoader.applicationContext.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(20); @@ -797,8 +796,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel @Override public void run() { if (audioRecorder != null) { - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } @@ -813,8 +816,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel try { if (startRecord(recordingAudioFile.getAbsolutePath()) == 0) { - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } audioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, recordBufferSize * 10); @@ -870,22 +877,24 @@ public class MediaController implements NotificationCenter.NotificationCenterDel } } - result[0] = false; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStartError); + } + }); return; } recordQueue.postRunnable(recordRunnable); - result[0] = true; - semaphore.release(); + Utilities.RunOnUIThread(new Runnable() { + @Override + public void run() { + NotificationCenter.getInstance().postNotificationName(recordStarted); + } + }); } - }, 120); - try { - semaphore.acquire(); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - return result[0]; + }, 100); } private void stopRecordingInternal(final boolean send) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index c9355a1ba..44fa0ddbe 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -342,6 +342,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().addObserver(this, MediaController.audioProgressDidChanged); NotificationCenter.getInstance().addObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().addObserver(this, MediaController.recordProgressChanged); + NotificationCenter.getInstance().addObserver(this, MediaController.recordStarted); + NotificationCenter.getInstance().addObserver(this, MediaController.recordStartError); NotificationCenter.getInstance().addObserver(this, 997); loading = true; @@ -388,6 +390,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa NotificationCenter.getInstance().removeObserver(this, MediaController.audioProgressDidChanged); NotificationCenter.getInstance().removeObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().removeObserver(this, MediaController.recordProgressChanged); + NotificationCenter.getInstance().removeObserver(this, MediaController.recordStarted); + NotificationCenter.getInstance().removeObserver(this, MediaController.recordStartError); NotificationCenter.getInstance().removeObserver(this, 997); if (sizeNotifierRelativeLayout != null) { sizeNotifierRelativeLayout.delegate = null; @@ -677,7 +681,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { startedDraggingX = -1; - recordingAudio = MediaController.getInstance().startRecording(dialog_id); + MediaController.getInstance().startRecording(dialog_id); updateAudioRecordIntefrace(); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { startedDraggingX = -1; @@ -2229,6 +2233,16 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa mActionMode.finish(); mActionMode = null; } + } else if (id == MediaController.recordStartError) { + if (recordingAudio) { + recordingAudio = false; + updateAudioRecordIntefrace(); + } + } else if (id == MediaController.recordStarted) { + if (!recordingAudio) { + recordingAudio = true; + updateAudioRecordIntefrace(); + } } }