1
0
mirror of https://github.com/NekoX-Dev/NekoX.git synced 2024-12-03 15:10:28 +01:00

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 audioProgressDidChanged = 50001;
public final static int audioDidReset = 50002; public final static int audioDidReset = 50002;
public final static int recordProgressChanged = 50003; 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<String, ArrayList<WeakReference<FileDownloadProgressListener>>> loadingFileObservers = new HashMap<String, ArrayList<WeakReference<FileDownloadProgressListener>>>();
private HashMap<Integer, String> observersByTag = new HashMap<Integer, String>(); private HashMap<Integer, String> observersByTag = new HashMap<Integer, String>();
@ -782,10 +784,7 @@ public class MediaController implements NotificationCenter.NotificationCenterDel
return isPaused; return isPaused;
} }
public boolean startRecording(final long dialog_id) { public void startRecording(final long dialog_id) {
final Semaphore semaphore = new Semaphore(0);
final Boolean[] result = new Boolean[1];
try { try {
Vibrator v = (Vibrator) ApplicationLoader.applicationContext.getSystemService(Context.VIBRATOR_SERVICE); Vibrator v = (Vibrator) ApplicationLoader.applicationContext.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(20); v.vibrate(20);
@ -797,8 +796,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel
@Override @Override
public void run() { public void run() {
if (audioRecorder != null) { if (audioRecorder != null) {
result[0] = false; Utilities.RunOnUIThread(new Runnable() {
semaphore.release(); @Override
public void run() {
NotificationCenter.getInstance().postNotificationName(recordStartError);
}
});
return; return;
} }
@ -813,8 +816,12 @@ public class MediaController implements NotificationCenter.NotificationCenterDel
try { try {
if (startRecord(recordingAudioFile.getAbsolutePath()) == 0) { if (startRecord(recordingAudioFile.getAbsolutePath()) == 0) {
result[0] = false; Utilities.RunOnUIThread(new Runnable() {
semaphore.release(); @Override
public void run() {
NotificationCenter.getInstance().postNotificationName(recordStartError);
}
});
return; return;
} }
audioRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 16000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, recordBufferSize * 10); 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; Utilities.RunOnUIThread(new Runnable() {
semaphore.release(); @Override
public void run() {
NotificationCenter.getInstance().postNotificationName(recordStartError);
}
});
return; return;
} }
recordQueue.postRunnable(recordRunnable); recordQueue.postRunnable(recordRunnable);
result[0] = true; Utilities.RunOnUIThread(new Runnable() {
semaphore.release(); @Override
public void run() {
NotificationCenter.getInstance().postNotificationName(recordStarted);
}
});
} }
}, 120); }, 100);
try {
semaphore.acquire();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return result[0];
} }
private void stopRecordingInternal(final boolean send) { 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.audioProgressDidChanged);
NotificationCenter.getInstance().addObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().addObserver(this, MediaController.audioDidReset);
NotificationCenter.getInstance().addObserver(this, MediaController.recordProgressChanged); NotificationCenter.getInstance().addObserver(this, MediaController.recordProgressChanged);
NotificationCenter.getInstance().addObserver(this, MediaController.recordStarted);
NotificationCenter.getInstance().addObserver(this, MediaController.recordStartError);
NotificationCenter.getInstance().addObserver(this, 997); NotificationCenter.getInstance().addObserver(this, 997);
loading = true; loading = true;
@ -388,6 +390,8 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa
NotificationCenter.getInstance().removeObserver(this, MediaController.audioProgressDidChanged); NotificationCenter.getInstance().removeObserver(this, MediaController.audioProgressDidChanged);
NotificationCenter.getInstance().removeObserver(this, MediaController.audioDidReset); NotificationCenter.getInstance().removeObserver(this, MediaController.audioDidReset);
NotificationCenter.getInstance().removeObserver(this, MediaController.recordProgressChanged); NotificationCenter.getInstance().removeObserver(this, MediaController.recordProgressChanged);
NotificationCenter.getInstance().removeObserver(this, MediaController.recordStarted);
NotificationCenter.getInstance().removeObserver(this, MediaController.recordStartError);
NotificationCenter.getInstance().removeObserver(this, 997); NotificationCenter.getInstance().removeObserver(this, 997);
if (sizeNotifierRelativeLayout != null) { if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.delegate = null; sizeNotifierRelativeLayout.delegate = null;
@ -677,7 +681,7 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa
public boolean onTouch(View view, MotionEvent motionEvent) { public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
startedDraggingX = -1; startedDraggingX = -1;
recordingAudio = MediaController.getInstance().startRecording(dialog_id); MediaController.getInstance().startRecording(dialog_id);
updateAudioRecordIntefrace(); updateAudioRecordIntefrace();
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) { } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
startedDraggingX = -1; startedDraggingX = -1;
@ -2229,6 +2233,16 @@ public class ChatActivity extends BaseFragment implements SizeNotifierRelativeLa
mActionMode.finish(); mActionMode.finish();
mActionMode = null; mActionMode = null;
} }
} else if (id == MediaController.recordStartError) {
if (recordingAudio) {
recordingAudio = false;
updateAudioRecordIntefrace();
}
} else if (id == MediaController.recordStarted) {
if (!recordingAudio) {
recordingAudio = true;
updateAudioRecordIntefrace();
}
} }
} }