remove main thread block before recording audio

This commit is contained in:
DrKLO 2014-03-23 04:09:24 +04:00
parent 433f59c5b9
commit e538243062
2 changed files with 43 additions and 20 deletions

View file

@ -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<String, ArrayList<WeakReference<FileDownloadProgressListener>>> loadingFileObservers = new HashMap<String, ArrayList<WeakReference<FileDownloadProgressListener>>>();
private HashMap<Integer, String> observersByTag = new HashMap<Integer, String>();
@ -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) {

View file

@ -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();
}
}
}