Cleaned the write to file method
This commit is contained in:
parent
d5d89af969
commit
319d91f04c
1 changed files with 66 additions and 28 deletions
|
|
@ -1,22 +1,12 @@
|
|||
package com.voixtreme.audiorecorder;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Locale;
|
||||
|
||||
import android.Manifest;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
||||
import android.media.AudioFormat;
|
||||
import android.media.AudioManager;
|
||||
import android.media.AudioRecord;
|
||||
|
|
@ -28,22 +18,28 @@ import android.os.Environment;
|
|||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.view.menu.ExpandedMenuView;
|
||||
import android.text.format.Time;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
|
||||
import com.dixitmobile.common.VerifyPermissions;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* jima (2017-05-23)
|
||||
*
|
||||
|
|
@ -282,6 +278,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener mStopRecordAudioClickListener = new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
|
@ -361,9 +358,9 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
|
||||
int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE
|
||||
, (isStereo ? AudioFormat.CHANNEL_IN_STEREO
|
||||
: AudioFormat.CHANNEL_IN_MONO)
|
||||
, AudioFormat.ENCODING_PCM_16BIT);
|
||||
, (isStereo ? AudioFormat.CHANNEL_IN_STEREO
|
||||
: AudioFormat.CHANNEL_IN_MONO)
|
||||
, AudioFormat.ENCODING_PCM_16BIT);
|
||||
mBuffer = new short[bufferSize];
|
||||
|
||||
final int audioSource;
|
||||
|
|
@ -416,9 +413,51 @@ public class MainActivity extends AppCompatActivity {
|
|||
mAudioSourceCb.setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void startBufferedWrite(final File file) {
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
DataOutputStream output = null;
|
||||
|
||||
try {
|
||||
output = new DataOutputStream(new BufferedOutputStream(
|
||||
new FileOutputStream(file)));
|
||||
|
||||
while (mIsRecording) {
|
||||
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
|
||||
|
||||
for (int frameIndex = 0; frameIndex < readSize; frameIndex++) {
|
||||
output.writeShort(mBuffer[frameIndex]);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Toast.makeText(MainActivity.this, e.getMessage()
|
||||
, Toast.LENGTH_SHORT).show();
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.flush();
|
||||
} catch (IOException e) {
|
||||
Toast.makeText(MainActivity.this, e.getMessage(),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
} finally {
|
||||
try {
|
||||
output.close();
|
||||
} catch (IOException e) {
|
||||
Toast.makeText(MainActivity.this, e.getMessage(),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void startBufferedWrite2(final File file) {
|
||||
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
|
@ -435,8 +474,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
while (mIsRecording) {
|
||||
double sum = 0;
|
||||
|
||||
int readSize = mRecorder.read(mBuffer, 0,
|
||||
mBuffer.length);
|
||||
int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
|
||||
|
||||
curSize += readSize;
|
||||
if (curSize > logSize) {
|
||||
|
|
@ -484,11 +522,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
}// end for(channel)
|
||||
|
||||
// mBuffer[frameIndex] *=iGain;
|
||||
if (mBuffer[frameIndex] > 32765) {
|
||||
mBuffer[frameIndex] = 32767;
|
||||
} else if (mBuffer[frameIndex] < -32767) {
|
||||
mBuffer[frameIndex] = -32767;
|
||||
}
|
||||
// if (mBuffer[frameIndex] > 32765) {
|
||||
// mBuffer[frameIndex] = 32767;
|
||||
// } else if (mBuffer[frameIndex] < -32767) {
|
||||
// mBuffer[frameIndex] = -32767;
|
||||
// }
|
||||
|
||||
output.writeShort(mBuffer[frameIndex]);
|
||||
sum += mBuffer[frameIndex] * mBuffer[frameIndex];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue