Skip to content

Commit a43b24c

Browse files
committed
don't send message if max volume below threshold
1 parent 9a3213e commit a43b24c

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,5 @@
331331
<string name="voice_help_3">Touch on a voice message to play, touch again to stop.</string>
332332
<string name="voice_help_4">New voice messages will play automatically for the open chat tab.</string>
333333
<string name="close">close</string>
334+
<string name="no_audio_detected">no audio detected - not sending voice message</string>
334335
</resources>

src/com/twofours/surespot/voice/VoiceController.java

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class VoiceController {
3939
private static String mFileName = null;
4040
private static String mUsername = null;
4141

42+
public static final int SEND_THRESHOLD = 5000;
4243
public static final int MAX_TIME = 10000;
4344
public static final int INTERVAL = 50;
4445

@@ -261,61 +262,69 @@ public synchronized static void stopRecording(Activity activity, boolean send) {
261262
}
262263

263264
private synchronized static void sendVoiceMessage(final Activity activity) {
264-
new AsyncTask<Void, Void, String>() {
265+
int maxVolume = mEnvelopeView.getMaxVolume();
266+
SurespotLog.v(TAG, "max recorded volume: %d", maxVolume);
267+
if (maxVolume < SEND_THRESHOLD) {
268+
Utils.makeToast(activity, activity.getString(R.string.no_audio_detected));
269+
}
270+
else {
271+
new AsyncTask<Void, Void, String>() {
265272

266-
@Override
267-
protected String doInBackground(Void... params) {
268-
// convert to AAC
269-
FileInputStream fis;
270-
try {
271-
fis = new FileInputStream(mFileName);
273+
@Override
274+
protected String doInBackground(Void... params) {
275+
// convert to AAC
276+
FileInputStream fis;
277+
try {
278+
fis = new FileInputStream(mFileName);
272279

273-
String outFile = File.createTempFile("record", ".aac").getAbsolutePath();
274-
mEncoder.init(16000, 1, mSampleRate, 16, outFile);
280+
String outFile = File.createTempFile("record", ".aac").getAbsolutePath();
281+
mEncoder.init(16000, 1, mSampleRate, 16, outFile);
275282

276-
mEncoder.encode(Utils.inputStreamToBytes(fis));
277-
mEncoder.uninit();
283+
mEncoder.encode(Utils.inputStreamToBytes(fis));
284+
mEncoder.uninit();
278285

279-
// convert to m4a (gingerbread can't play the AAC for some bloody reason).
280-
final String m4aFile = File.createTempFile("record", ".m4a").getAbsolutePath();
281-
new AACToM4A().convert(activity, outFile, m4aFile);
286+
// convert to m4a (gingerbread can't play the AAC for some bloody reason).
287+
final String m4aFile = File.createTempFile("record", ".m4a").getAbsolutePath();
288+
new AACToM4A().convert(activity, outFile, m4aFile);
282289

283-
// delete files
284-
new File(outFile).delete();
285-
new File(mFileName).delete();
290+
// delete files
291+
new File(outFile).delete();
292+
new File(mFileName).delete();
286293

287-
return m4aFile;
294+
return m4aFile;
288295

289-
}
296+
}
290297

291-
catch (IOException e) {
292-
SurespotLog.w(TAG, e, "sendVoiceMessage");
298+
catch (IOException e) {
299+
SurespotLog.w(TAG, e, "sendVoiceMessage");
300+
}
301+
return null;
293302
}
294-
return null;
295-
}
296303

297-
protected void onPostExecute(final String encryptedVoiceMessageFile) {
298-
if (encryptedVoiceMessageFile != null) {
304+
protected void onPostExecute(final String encryptedVoiceMessageFile) {
305+
if (encryptedVoiceMessageFile != null) {
299306

300-
ChatUtils.uploadVoiceMessageAsync(activity, MainActivity.getChatController(), MainActivity.getNetworkController(),
301-
Uri.fromFile(new File(encryptedVoiceMessageFile)), mUsername, new IAsyncCallback<Boolean>() {
307+
ChatUtils.uploadVoiceMessageAsync(activity, MainActivity.getChatController(), MainActivity.getNetworkController(),
308+
Uri.fromFile(new File(encryptedVoiceMessageFile)), mUsername, new IAsyncCallback<Boolean>() {
302309

303-
@Override
304-
public void handleResponse(Boolean result) {
305-
if (result) {
306-
// delete m4a
307-
new File(encryptedVoiceMessageFile).delete();
310+
@Override
311+
public void handleResponse(Boolean result) {
312+
if (result) {
313+
// delete m4a
314+
new File(encryptedVoiceMessageFile).delete();
315+
}
308316
}
309-
}
310-
});
317+
});
311318

312-
}
313-
else {
314-
Utils.makeToast(activity, "error sending message");
315-
}
319+
}
320+
else {
321+
Utils.makeToast(activity, "error sending message");
322+
}
316323

317-
};
318-
}.execute();
324+
};
325+
326+
}.execute();
327+
}
319328

320329
}
321330

src/com/twofours/surespot/voice/VolumeEnvelopeView.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
public class VolumeEnvelopeView extends View {
1515
private static final String TAG = null;
16+
private static int mMaxVolume;
1617

1718
/**
1819
* Constructor. This version is only needed if you will be instantiating the object manually (not from a layout XML file).
@@ -70,6 +71,9 @@ public void setNewVolume(int value, boolean redraw) {
7071

7172
if (value != 0) {
7273
mEnvelope.add(value);
74+
if (value > mMaxVolume) {
75+
mMaxVolume = value;
76+
}
7377
}
7478
else
7579
if (!mEnvelope.isEmpty())
@@ -83,6 +87,7 @@ public void setNewVolume(int value, boolean redraw) {
8387
}
8488

8589
public void clearVolume() {
90+
mMaxVolume = 0;
8691
mEnvelope.clear();
8792
invalidate();
8893
}
@@ -108,4 +113,8 @@ protected void onDraw(Canvas canvas) {
108113
private Paint mEnvelopePaint;
109114
private LinkedList<Integer> mEnvelope = new LinkedList<Integer>();
110115
int mSize = 0;
116+
117+
public int getMaxVolume() {
118+
return mMaxVolume;
119+
}
111120
}

0 commit comments

Comments
 (0)