I am trying to run the audio recording http://developer.android.com/guide/topics/media/index.html, Its working fine, what I need is to show max amplitude while recording voice continuously. What is the best approach for that.
Max amplitude gives max amplitude of the given sample, so I taken sample for every 250 milli seconds and calculated max amplitude
public void run() {
int i = 0;
while(i == 0) {
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
try {
sleep(250);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (mRecorder != null) {
amplitude = mRecorder.getMaxAmplitude();
b.putLong("currentTime", amplitude);
Log.i("AMPLITUDE", new Integer(amplitude).toString());
} else {
b.putLong("currentTime", 0);
}
msg.setData(b);
mHandler.sendMessage(msg);
}
}
I used message handlers to modify front end using background process thread
Create a thread which runs all the time.
In the thread do this:
int amp = mrec.getMaxAmplitude();
if (amp > 0)
yourcode;
Do you need more information on the thread?
Related
I writing an android timer app that uses System.nanotime. The issue is that it's giving me underpredicted results as well negative numbers. redVal, blueVal, and greenVal are updated on each frame of the camera.
results
504455566
-95947265
9063721
61035
-99487305
-98937988
12664795
-75317382
code
for (testAmount = 0; testAmount < 80; testAmount++) {
runOnUiThread(new Runnable() {
public void run() {
lagSquare.setBackgroundColor(Color.rgb(255, 255, 255));
lagStartTime = System.nanoTime(); //start lagTimer start
}
});
while (redVal <= 100.0 && blueVal <= 100.0 && greenVal <= 100.0) {
x=0;
}
runOnUiThread(new Runnable() {
public void run() {
lagEndTime = System.nanoTime(); //start lagTimer end
lagSquare.setBackgroundColor(Color.rgb(000, 000, 000));//set lagSquare black
}
});
lagTimeResult = (lagEndTime - lagStartTime);
timeArray[testAmount] = lagTimeResult;
Log.i("LTR", String.valueOf(lagTimeResult));
try {
Thread.sleep(60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
You are trying to output a time difference which is relying upon values being set in different threads, without any synchronization. This will almost always end up with the wrong value:
for (testAmount = 0; testAmount < 80; testAmount++) {
// this will schedule the Runnable to run *sometime* in
// the future - but not necessarily right now
runOnUiThread(new Runnable() {
public void run() {
lagStartTime = System.nanoTime(); //start lagTimer start
}
});
// this will also schedule this Runnable to run *sometime* in
// the future - but not necessarily after the first one
runOnUiThread(new Runnable() {
public void run() {
lagEndTime = System.nanoTime(); //start lagTimer end
}
});
// this will probably execute first (before either of
// the other Runnables have completed)
lagTimeResult = (lagEndTime - lagStartTime);
}
You simply can't rely on the order of threads executing in the order you've coded them - and definitely not in a loop.
I can't understand from your question what you are trying to time, but the take-home rule is that whenever you have multiple threads, you can never rely on the order of execution without using some form of synchronization.
I have setup two commands in Oncreate and both send data to a bluetooth device. I need the 2nd command to wait for a data string to be received from the 1st command before advancing to the 2nd command. Each command just send a byte to BT. I tried a while looping true but does not seam to work and hangs on the while true statement. I assume the while true is not letting the handler fire when in the loop. Both commands work fine individually as long as I don't send both.
This is the code in Oncreate with both commands and while true statement
looping=true;
intByteCount =9;
GetData(intCommand); // (Command 1)Send byte to get data on reveiver
while( looping) { // Wait add data to be received before next command
Log.d("TAG", "On Hold ? ");
}
intByteCount=160; // (command 2)
GetTitle(intCommand);
This is the code in a handler for bluetooth that sets the looping to false once all the bytes have been received.
Handler h = new Handler() {
#Override
// public void handleMessage(android.os.Message msg) {
public void handleMessage(android.os.Message msg) {
byte[]readBuf = (byte[]) msg.obj;
if (intByteCount==9){
// Data is channel status and Master value
byte[] encodedBytes = new byte[5];
System.arraycopy(readBuf, 0, encodedBytes, 0, encodedBytes.length);
looping=false;
};
The GetTitle and GetData are basically the same
Here is the GetTitle()
private void GetData(int FixtureNumber) {
Log.d("TAG", "Value " + intArrayToInt(intArray1));
intByteCount=9; // set to receive 9 bytes
byte buffer[] = new byte[6];
buffer[0] = ((byte) 1); // Command (get data)
buffer[1] = ((byte) Master_value);
buffer[2] = ((byte) intArrayToInt(intArray1));
buffer[3] = ((byte) intArrayToInt(intArray2));
buffer[4] = ((byte) 3);
buffer[5] = ((byte) 4);
if (isBTConnected) {
try {
mmOutputStream.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the final code to get both control data
new Thread(new Runnable() {
#Override
public void run() {
intByteCount =9;
GetData(intCommand); // Send byte to get data on reveiver
//you can use a for here and check if the command was executed or just wait and execute the 2nd command
try {
Thread.sleep(1000); //wait 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
intByteCount=160; // Sed incoming data byte count
GetTitle(intCommand);
}
}).start();
You can use a Thread and wait x amount of time, :
new Thread(new Runnable() {
#Override
public void run() {
//your 1st command
//you can use a for here and check if the command was executed or just wait and execute the 2nd command
try {
Thread.sleep(2000); //wait 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
//your 2nd command
}
}).start();
Thank you Agustin that worked fine. The received data was quick so just a delay will work without the control boolean. Here is the new code.
new Thread(new Runnable() {
#Override
public void run() {
intByteCount =9;
GetData(intCommand); // Send byte to get data on reveiver
//you can use a for here and check if the command was executed or just wait and execute the 2nd command
try {
Thread.sleep(1000); //wait 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
intByteCount=160; // Sed incoming data byte count
GetTitle(intCommand);
}
}).start();
You are currently holding up the main thread there, something it is not advised to do. The best option for this is to modify the code so that the second Bluetooth command is done in a function that is called where the looping == false code is used.
I am trying to record audio from the device.
I created an AudioRecord object and manage it over the cycle of the activity.
When my app goes to background it stops, and when in foreground it continues.
When the recording is running, I want to get the samples from the recorder to a byte array
This is the code I use to do it:
private void startRecorder() {
Log.d(TAG, "before start recording");
myBuffer = new byte[2048];
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_DTMF, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
myRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 2048);
myThread = new Thread() {
public void run() {
while (true) {
if (myRecorder != null && myRecorder.getState() == AudioRecord.RECORDSTATE_RECORDING) {
myRecorder.read(myBuffer, 0, 2048);
recordingSampleNumber++;
if (recordingSampleNumber % 10 == 0) {
Log.d(TAG, "recording sample number:" + recordingSampleNumber);
}
}
}
}
};
myThread.setPriority(Thread.MAX_PRIORITY);
myRecorder.startRecording();
myThread.start();
Log.d(TAG, "after start recording");
}
My problem is: Every once in a while I get the following error :
06-22 11:44:21.057: E/AndroidRuntime(17776): Process: com.example.microphonetestproject2, PID: 17776
06-22 11:44:21.057: E/AndroidRuntime(17776): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.AudioRecord.getState()' on a null object reference
06-22 11:44:21.057: E/AndroidRuntime(17776): at com.example.microphonetestproject2.MicrophoneTestApp$3.run(MicrophoneTestApp.java:108)
my question is: Why would I get an NPE on myRecorder.getState() when just half a line before that i wrote "if myRecorder!=null"
This looks like a concurrency problem.
It seems that after the check myRecorder != null, the variable is actually set to null in a different thread, which is possible because as you probably know threads run in parallel.
I'd recommend you lock on the object and execute your loop. Then, no one will access it out of place, i.e. unexpectedly.
while (true) {
synchronized (myRecorder) {
if (myRecorder != null && myRecorder.getState() == AudioRecord.RECORDSTATE_RECORDING) {
myRecorder.read(myBuffer, 0, 2048);
recordingSampleNumber++;
if (recordingSampleNumber % 10 == 0) {
Log.d(TAG, "recording sample number:" + recordingSampleNumber);
}
}
}
}
Although this may fix your problem, you should deal with it with in other ways, e.g. instead of setting the variable to null to cancel the thread, use the built in interrupt and join methods:
private Thread mRecorderThread;
private void startRecorder() {
myRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 2048);
mRecorderThread = new Thread() {
public void run() {
while (true) {
if (!isInterrupted() && myRecorder.getState() == AudioRecord
.RECORDSTATE_RECORDING) {
myRecorder.read(myBuffer, 0, 2048);
recordingSampleNumber++;
if (recordingSampleNumber % 10 == 0) {
Log.d(TAG, "recording sample number:" + recordingSampleNumber);
}
}
}
}
};
mRecorderThread.setPriority(Thread.MAX_PRIORITY);
myRecorder.startRecording();
mRecorderThread.start();
Log.d(TAG, "after start recording");
}
private void stopRecorder() {
mRecorderThread.interrupt();
// Wait for the thread to finish (for the interruption to take effect)
try {
mRecorderThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
myRecorder.stop();
}
interrupt() as you may have understood, interrupts the thread, but it does not kill it instantly. With the help of join() you can wait for the Thread to finish after you interrupted it.
I want to play a horn sound in the app without any lag. I am using Media player class but its giving lag while playing file again.
code to run:
thread to improve the lag:(mp_horn is the media player instance we made from sound file) Below thread gives us much better result then making mediaplayer.setloop(true)
#Override
public void run() {
try {
if (mp_horn != null && mp_horn.isPlaying()) {
final long durationTotal_horn = mp_horn
.getDuration();
long durationCurrent_horn = mp_horn
.getCurrentPosition();
if (durationCurrent_horn >= (.90) * durationTotal_horn) {
// mp_engineContiue.seekTo((int)
// durationCurrent_back);
Log.v("arrrrrr", durationCurrent_horn
+ "......."
+ durationTotal_horn);
// mp_engineContiue.pause();
mp_horn.seekTo((int) (durationTotal_horn * .0000001));
}
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Probably
if (mp_horn != null && mp_horn.isPlaying())
should be :
if (mp_horn != null && !mp_horn.isPlaying())
otherwise while the Horn is playing you are doing all these calculations all over again, which I am guessing is causing the lag
I would like to know if there is a way to fix the duration of recording using mobile's microphone. Like when I click a button the recording should start and it should stop after 5 seconds on its own, what method do you propose me to use :-)
Edit:
Sorry for the confusion but I am using AudioRecorder class to record data and I don't think the MediaRecorder class function works properly (/at all) for the same.
If you just use a timer, I do not think that you can accurately control how much data is within the buffer when your app reads it.
I think they way to record 5 seconds of audio data is to use the technique from this class.
The code there carefully sets the size of the audio buffer so that it will call back after it has recorded data for a certain amount of time. Here is a snipped from that class.
public boolean startRecordingForTime(int millisecondsPerAudioClip,
int sampleRate, int encoding)
{
float percentOfASecond = (float) millisecondsPerAudioClip / 1000.0f;
int numSamplesRequired = (int) ((float) sampleRate * percentOfASecond);
int bufferSize =
determineCalculatedBufferSize(sampleRate, encoding,
numSamplesRequired);
return doRecording(sampleRate, encoding, bufferSize,
numSamplesRequired, DEFAULT_BUFFER_INCREASE_FACTOR);
}
Then later on your code just does this:
while (continueRecording)
{
int bufferResult = recorder.read(readBuffer, 0, readBufferSize);
//do stuff
}
since readBufferSize is just right, you will get the amount of data you want (with some slight variation)
This is all what you need.
#Override
public void onClick(View view)
{
if (view.getId() == R.id.Record)
{
new Timer().schedule(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
files.setEnabled(true);
record.setEnabled(true);
stop.setEnabled(false);
}
});
}
}, 5000);
record.setEnabled(false);
files.setEnabled(false);
stop.setEnabled(true);
try
{
File file = new File(Environment.getExternalStorageDirectory(),
"" + new Random().nextInt(50) + ".3gp");
adapter.add(file.getAbsolutePath());
adapter.notifyDataSetChanged();
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
stop.setEnabled(true);
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
Use setMaxDuration from MediaRecorder class.
alternately
When you start recording start a new thread and put it to sleep for 5 seconds. when it wakes stop the recording.
or use a timertask which shall call the stop recording after 5 second delay.
or