Why does this metronome app crash? (Android) - android

I'm working on a very small Android Project that uses this exact code from github.
However, when I (or you) intermittently button mash the start/stop button... the app eventually crashes. Unfortunately this can take a little while to reproduce... but it will happen!
Oh, I forgot the desired result!!
The desired result is that this crash does not occur. :)
Does anyone know why this crash occurs? The author of this code has had an open bug/issue for this on Github since March of 2013... so I'm pretty sure it's not a particularly stupid question... and if you do know the answer to this, you would no doubt be a hailed as a bowss.
I have been dissecting the code, print debugging, and researching ASyncTask, Handlers, and AudioTrack for a couple of days now but I can't figure it out... I will though if nobody else beats me to it.
This is the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: com.example.boober.beatkeeper, PID: 15664
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Unable to retrieve AudioTrack pointer for write()
at android.media.AudioTrack.native_write_byte(Native Method)
at android.media.AudioTrack.write(AudioTrack.java:1761)
at android.media.AudioTrack.write(AudioTrack.java:1704)
at com.example.boober.beatkeeper.AudioGenerator.writeSound(AudioGenerator.java:55)
at com.example.boober.beatkeeper.Metronome.play(Metronome.java:60)
at com.example.boober.beatkeeper.MainActivity$MetronomeAsyncTask.doInBackground(MainActivity.java:298)
at com.example.boober.beatkeeper.MainActivity$MetronomeAsyncTask.doInBackground(MainActivity.java:283)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
You could just go to github and download the original code, but in order to satisfy stackoverflow requirements, I have also provided the even-more-concise "minimal working example" which you can individually cut and paste into your Android Studio if you prefer.
MainActivity:
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String TAG = "AAA";
Button playStopButton;
TextView currentBeat;
// important objects
MetronomeAsyncTask aSync;
Handler mHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentBeat = findViewById(R.id.currentBeatTextView);
playStopButton = findViewById(R.id.playStopButton);
// important objcts
aSync = new MetronomeAsyncTask();
}
// only called from within playStopPressed()
private void stopPressed() {
aSync.stop();
aSync = new MetronomeAsyncTask();
}
// only called from within playStopPressed()
private void playPressed() {
//aSync.execute();
aSync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
}
public synchronized void playStopButtonPressed(View v) {
boolean wasPlayingWhenPressed = playStopButton.isSelected();
playStopButton.setSelected(!playStopButton.isSelected());
if (wasPlayingWhenPressed) {
stopPressed();
} else {
playPressed();
}
}
// METRONOME BRAIN STUFF ------------------------------------------
private Handler getHandler() {
return new Handler() {
#Override
public void handleMessage(Message msg) {
String message = (String) msg.obj;
if (message.equals("1")) {
currentBeat.setTextColor(Color.GREEN);
}
else {
currentBeat.setTextColor(Color.BLUE);
}
currentBeat.setText(message);
}
};
}
private class MetronomeAsyncTask extends AsyncTask<Void, Void, String> {
MetronomeBrain metronome;
MetronomeAsyncTask() {
mHandler = getHandler();
metronome = new MetronomeBrain(mHandler);
Runtime.getRuntime().gc(); // <---- don't know if this line is necessary or not.
}
protected String doInBackground(Void... params) {
metronome.setBeat(4);
metronome.setNoteValue(4);
metronome.setBpm(100);
metronome.setBeatSound(2440);
metronome.setSound(6440);
metronome.play();
return null;
}
public void stop() {
metronome.stop();
metronome = null;
}
public void setBpm(short bpm) {
metronome.setBpm(bpm);
metronome.calcSilence();
}
public void setBeat(short beat) {
if (metronome != null)
metronome.setBeat(beat);
}
}
}
MetronomeBrain:
import android.os.Handler;
import android.os.Message;
public class MetronomeBrain {
private double bpm;
private int beat;
private int noteValue;
private int silence;
private double beatSound;
private double sound;
private final int tick = 1000; // samples of tick
private boolean play = true;
private AudioGenerator audioGenerator = new AudioGenerator(8000);
private Handler mHandler;
private double[] soundTickArray;
private double[] soundTockArray;
private double[] silenceSoundArray;
private Message msg;
private int currentBeat = 1;
public MetronomeBrain(Handler handler) {
audioGenerator.createPlayer();
this.mHandler = handler;
}
public void calcSilence() {
silence = (int) (((60 / bpm) * 8000) - tick);
soundTickArray = new double[this.tick];
soundTockArray = new double[this.tick];
silenceSoundArray = new double[this.silence];
msg = new Message();
msg.obj = "" + currentBeat;
double[] tick = audioGenerator.getSineWave(this.tick, 8000, beatSound);
double[] tock = audioGenerator.getSineWave(this.tick, 8000, sound);
for (int i = 0; i < this.tick; i++) {
soundTickArray[i] = tick[i];
soundTockArray[i] = tock[i];
}
for (int i = 0; i < silence; i++)
silenceSoundArray[i] = 0;
}
public void play() {
calcSilence();
do {
msg = new Message();
msg.obj = "" + currentBeat;
if (currentBeat == 1)
audioGenerator.writeSound(soundTockArray);
else
audioGenerator.writeSound(soundTickArray);
if (bpm <= 120)
mHandler.sendMessage(msg);
audioGenerator.writeSound(silenceSoundArray);
if (bpm > 120)
mHandler.sendMessage(msg);
currentBeat++;
if (currentBeat > beat)
currentBeat = 1;
} while (play);
}
public void stop() {
play = false;
audioGenerator.destroyAudioTrack();
}
public double getBpm() {
return bpm;
}
public void setBpm(int bpm) {
this.bpm = bpm;
}
public int getNoteValue() {
return noteValue;
}
public void setNoteValue(int bpmetre) {
this.noteValue = bpmetre;
}
public int getBeat() {
return beat;
}
public void setBeat(int beat) {
this.beat = beat;
}
public double getBeatSound() {
return beatSound;
}
public void setBeatSound(double sound1) {
this.beatSound = sound1;
}
public double getSound() {
return sound;
}
public void setSound(double sound2) {
this.sound = sound2;
}
}
AudioGenerator:
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
public class AudioGenerator {
private int sampleRate;
private AudioTrack audioTrack;
public AudioGenerator(int sampleRate) {
this.sampleRate = sampleRate;
}
public double[] getSineWave(int samples,int sampleRate,double frequencyOfTone){
double[] sample = new double[samples];
for (int i = 0; i < samples; i++) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/frequencyOfTone));
}
return sample;
}
public byte[] get16BitPcm(double[] samples) {
byte[] generatedSound = new byte[2 * samples.length];
int index = 0;
for (double sample : samples) {
// scale to maximum amplitude
short maxSample = (short) ((sample * Short.MAX_VALUE));
// in 16 bit wav PCM, first byte is the low order byte
generatedSound[index++] = (byte) (maxSample & 0x00ff);
generatedSound[index++] = (byte) ((maxSample & 0xff00) >>> 8);
}
return generatedSound;
}
public void createPlayer(){
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, sampleRate,
AudioTrack.MODE_STREAM);
audioTrack.play();
}
public void writeSound(double[] samples) {
byte[] generatedSnd = get16BitPcm(samples);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
}
public void destroyAudioTrack() {
audioTrack.stop();
// This line seems to be a most likely culprit of the start/stop crash.
// Is this line even necessary?
audioTrack.release();
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.boober.android_metronome.MainActivity">
<Button
android:id="#+id/playStopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="playStopButtonPressed"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/currentBeatTextView"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text="TextView"
android:gravity="center_vertical"
android:textAlignment="center"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playStopButton" />
</android.support.constraint.ConstraintLayout>

After thinking about dmarin's comment and reading the code, I arrive to the conclusion that dmarin answered your question. It's a race condition, and it's also an access of an object which is not initialized. So the short solution is: The code needs to check, if the accessed data is initialized. The AudioTrack objects can be checked, if it is null or if the getState() equals "initialized". Unfortunately, the problem does not disappear with my setup (Android Studio 3.1.2, Android SDK Build-Tools 28-rc2).
private boolean isInitialized() {
return audioTrack.getState() == AudioTrack.STATE_INITIALIZED;
}
After a code analysis one could notice the creation of AsyncTasks and AudioTracks. So, to minimize those, create the AsyncTask only once in the onCreate - function and set the AudioTrack object to static.
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentBeat = findViewById(R.id.currentBeatTextView);
playStopButton = findViewById(R.id.playStopButton);
// important objcts
aSync = new MetronomeAsyncTask();
aSync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
}
AudioGenerator
public class AudioGenerator {
/*changed to static*/
private static AudioTrack audioTrack;
...
}
I admit just changing it to static is not a beautiful solution. But since I only need one pipe to the AudioService, this will do.
Creating the audio-pipe, stopping the playing of the audio and freeing the resource will look like this:
public void createPlayer(){
if (audioTrack == null || ! isInitialized())
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, sampleRate,
AudioTrack.MODE_STREAM);
if (isInitialized()){
audioTrack.play();
}
}
public void destroyAudioTrack() {
if (isInitialized()) {
audioTrack.stop();
}
}
public void stopRelease() {
if (isInitialized()) {
audioTrack.stop();
audioTrack.release();
}
}
The boolean play is repurposed by me. Also, the beat counter called currentBeat is reset, when the play button is pressed. For accessing from the MainActivity: The change from private to public of those variables is not the best solution.
// only called from within playStopPressed()
private void stopPressed() {
aSync.metronome.play = false;
}
// only called from within playStopPressed()
private void playPressed() {
aSync.metronome.play = true;
aSync.metronome.currentBeat = 1;
}
In play() of MetronomeBrain the loop becomes an endless loop. This problem will be fixed, soon. That is why the play boolean may be repurposed. The playing of the tones needs to be set to a different condition, which depends on play.
public void play() {
calcSilence();
/*a change for the do-while loop: It runs forever and needs
to be killed externally of the loop.
Also the play decides, if audio is being played.*/
do {
msg = new Message();
msg.obj = "" + currentBeat;
if (currentBeat == 1 && play)
audioGenerator.writeSound(soundTockArray);
else if (play)
audioGenerator.writeSound(soundTickArray);
if (bpm <= 120)
mHandler.sendMessage(msg);
audioGenerator.writeSound(silenceSoundArray);
if (bpm > 120)
mHandler.sendMessage(msg);
currentBeat++;
if (currentBeat > beat)
currentBeat = 1;
} while (true);
}
Now the loop runs forever, but it may only play, if play is set to true. If the clean-up is necessary, it may be done at the end of the Activity lifecycle like this in the MainActivity:
#Override
protected void onDestroy() {
aSync.metronome.stopReleaseAudio(); //calls the stopRelease()
aSync.cancel(true);
super.onDestroy();
}
As I stated, the code could be further improved, but it gives a fair hint and enough material to think/learn about AsyncTasks, Services like Audio Service and Activity - Lifecycles.
References
- https://developer.android.com/reference/android/os/AsyncTask
- https://developer.android.com/reference/android/media/AudioManager
- https://developer.android.com/reference/android/media/AudioTrack
- https://developer.android.com/reference/android/app/Activity#activity-lifecycle
TL;DR: Make sure that the objects are initialized before accessing them, just create everything once, and destroy them, when you do not need them e.g. at the end of the activity.

Related

Android App Crashes with while loop

I'm making this app that tells how much longer till we reach a destination but whenever I try to use it it just black screens just crashes. I think it has something to do with the loop but I can't find anything online Can anyone help? Thanks in advance.................................................................................................................
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Calendar;
import java.util.TimeZone;
import java.time.*;
import java.math.*;
public class TimeTIll extends AppCompatActivity {
private static final String DateUtils = null;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_till);
txt = (TextView) findViewById(R.id.TimeDipslay);
int i = 0;
while (i < 10) {
Program();
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
public void Program() {
int totalMinutesSinceMidnight = 0;
long ms = 0;
long secondsM = 0;
long SecondsTillEstimate = 0;
long SecondsTillArrival1 = 0;
double MinutesTillArrival = 0;
double hoursTillArrival = 0;
double SecondsTillArrival = 0;
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.set(Calendar.HOUR_OF_DAY, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);
midnight.set(Calendar.MILLISECOND, 0);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
now = Calendar.getInstance();
midnight = Calendar.getInstance();
ms = now.getTime().getTime() - midnight.getTime().getTime();
totalMinutesSinceMidnight = (int) (ms / 1000);
secondsM = totalMinutesSinceMidnight;
SecondsTillEstimate = (60 * 60 * 23);
SecondsTillArrival1 = SecondsTillEstimate - secondsM;
hoursTillArrival = Math.floor(SecondsTillArrival1 / 3600);
SecondsTillArrival1 -= (hoursTillArrival * 3600);
MinutesTillArrival = Math.floor(SecondsTillArrival1 / 60);
SecondsTillArrival1 -= (MinutesTillArrival * 60);
SecondsTillArrival = SecondsTillArrival1;
txt.setText((int) hoursTillArrival + " Hours " + (int) MinutesTillArrival + " Minutes " + (int) SecondsTillArrival + " Seconds Till Arrival In Floriada");
}
}
Please read about the activity life-cycle. You cannot call long-running tasks in onCreate and expect them to show anything other than an ANR crash (your onCreate method blocks the UI thread with all those sleep statements and generates an ANR crash before it even gets to the onStart and onResume stages where it actually shows the activity).
If you want the UI to change dynamically, you could use a handler to execute a method at a regular interval. For example, to update something at a regular interval (1000 ms here) you could do:
public class MainActivity extends AppCompatActivity {
private final Handler handler = new Handler();
private TextView txt;
private int count = 0;
private final int updateFreqMs = 1000; // call update every 1000 ms
#Override
protected void onPause() {
super.onPause();
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onResume() {
super.onResume();
handler.postDelayed(new Runnable() {
#Override
public void run() {
updateTime();
handler.postDelayed(this, updateFreqMs);
}
}, updateFreqMs);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_till);
txt = findViewById(R.id.TimeDipslay);
}
private void updateTime() {
// here you update the displayed value
// this will be called every second indefinitely
// do your math and generate a string to print, just showing
// a counter here to demonstrate
++count;
txt.setText("Count = " + count);
}
}
I think that the problem is that you are trying to make sleep the main thread of the application. In Android I knew that for security reasons this can't be done.
(Look this https://www.codementor.io/tips/0743378261/non-freezing-sleep-in-android-app for details )
Try this solution instead:
How to pause / sleep thread or process in Android?
You Shouldn`t use it inside main thread specially when you are doing operation inside it.
You may have to use some thing like that.
viewModelScope.launch(Dispatchers.IO) {
while (statues==true){
if (statue==true){
delay(500)
//ToDo
}
}
}

ANDROID: How would I add a delay in a for loop which plays an audiofile?

After a particular button is clicked, I want to have it so that my audio would play fifteen times, and have the progress bar increment each time. I had also envisioned having a delay in between each time the audio plays.
What's currently happening is that all of the beeps play back to back without delay, and after that, the progress bar gets incremented right to max straight away. Using Handler somehow doesn't manage to delay the audio playing.
I'm a beginner in app development, so excuse the shoddy code:
public void click1(View view) throws IOException {
int i;
// ProgressBar1.setProgress(0);
for (i = 1; i < 16; i = i + 1)
{
int secs = 2; // Delay in seconds
Utils.delay(secs, new Utils.DelayCallback() {
#Override
public void afterDelay() throws IOException {
// Do something after delay
PlayShortAudioFileViaAudioTrack(500, 1);
ProgressBar1.incrementProgressBy(1);
}
});
}
}
Here's the delay code:
public class Utils {
public interface DelayCallback{
void afterDelay() throws IOException;
}
public static void delay(int secs, final DelayCallback delayCallback){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
try {
delayCallback.afterDelay();
} catch (IOException e) {
e.printStackTrace();
}
}
}, secs * 1000); // afterDelay will be executed after (secs*1000) milliseconds.
}
}
The function that plays audio is
public void PlayShortAudioFileViaAudioTrack(int f, double duration) throws IOException
{ int sampleRate = 48000; // Samples per second
//double duration = 2.0;
long numFrames = (long)(duration * sampleRate);
long frameCounter = 0;
int intSize = android.media.AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_FLOAT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_FLOAT, intSize, AudioTrack.MODE_STREAM);
float[] buffer = new float[intSize];
while (frameCounter < numFrames)
{
long remaining = numFrames - frameCounter;
int toWrite = (remaining > intSize) ? intSize : (int) remaining;
for (int s=0 ; s<toWrite ; s++, frameCounter++)
{
buffer[s] = (float)Math.sin(2.0 * Math.PI * f * frameCounter / sampleRate);
// buffer[1][s] = Math.sin(2.0 * Math.PI * 500 * frameCounter / sampleRate);
}
if (at!=null) {
// Write the byte array to the track
at.play();
at.write(buffer, 0, intSize, AudioTrack.WRITE_BLOCKING);
}
else
Log.d("TCAudio", "audio track is not initialised ");
}
at.stop();
at.release();
}
Changing the audiotrack mode to NON-BLOCKING from BLOCKING results in the audio just playing once, and the progress bar still shooting up to full immediately.
To solve your problem, you can use AsynkTask<> like this:
Create a subclass of AsynkTask<> in your Activity to handle the delayed action and the updates of the progressbar.
Then in your click1()-method you just have to create a new instance of your AsyncTask subclass and execute it. You can give it the number of cycles on the call of execute(). The following code should work:
...
ProgressBar1.setMax(16); // to get 16 cycles like in your example
...
public void click1(View view) throws IOException {
int max = ProgressBar1.getMax();
new MyTask().execute(max);
}
class MyTask extends AsyncTask<Integer, Integer, Void> {
#Override
protected Void doInBackground(Integer... params) {
for (int i=0 ; i <= params[0]; i++) {
try {
Thread.sleep(secs * 1000);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPreExecute() {
//do something before execution
}
#Override
protected void onProgressUpdate(Integer... values) {
//do your delayed stuff
PlayShortAudioFileViaAudioTrack(500, 1);
ProgressBar1.incrementProgressBy(1);
}
}

Android: Is there any intents to listen for CPU "sleep" and "wake Up" apart from screen on and off?

My understanding is,existing Screen OFF and ON intents are not exactly mean that the device is in sleep and waked up respectively. Any applications on the device holds partial wake lock, device will not be in deep sleep but screen may be off/on.
Is there any intents to listen CPU "WAKE UP" and "SLEEP" ?
Is there any way, we know CPU is waked UP from deep sleep ?
I needed a tool to do exactly this when troubleshooting some timing behavior on my app in the background. So I made my own class to do it. See code below. Here's how you use it:
CpuSleepDetector.getInstance().setSleepEndNotifier(new CpuSleepDetector.SleepEndNotifier() {
#Override
public void cpuSleepEnded(long sleepDurationMillis) {
Log.d(TAG, "The CPU just exited sleep. It was sleeping for "+sleepDurationMillis+" ms.");
}
});
CpuSleepDetector.getInstance().logDump();
The logDump method will dump the last 100 sleep events to LogCat. This is useful in troubleshooting, becaue to get the CPU to sleep, I had to not only disconnect my USB cable from my phone, I actually had to turn off my adb connection over WiFi. This way, you can reconnect adb at a later time and use the logDump method to get recent detections.
I know this is an old question, but hopefully this will be useful to somebody else.
Here's the code for the detector class:
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
public class CpuSleepDetector {
private static final String TAG = CpuSleepDetector.class.getSimpleName();
private static CpuSleepDetector instance = null;
private HandlerThread thread;
private Handler handler;
private SleepEndNotifier notifier;
public static CpuSleepDetector getInstance() {
if (instance == null) {
instance = new CpuSleepDetector();
}
return instance;
}
private CpuSleepDetector() {
thread = new HandlerThread("cpuSleepDetectorThread");
thread.start();
handler = new Handler(thread.getLooper());
watchForSleep();
}
private void watchForSleep(){
// uptime stalls when cpu stalls
final long uptimeAtStart = SystemClock.uptimeMillis();
final long realtimeAtStart = SystemClock.elapsedRealtime();
handler.postDelayed(new Runnable() {
#Override
public void run() {
long uptimeAtEnd = SystemClock.uptimeMillis();
long realtimeAtEnd = SystemClock.elapsedRealtime();
long realtimeDelta = realtimeAtEnd - realtimeAtStart;
long uptimeDelta = uptimeAtEnd - uptimeAtStart;
final long sleepTime = realtimeDelta - uptimeDelta;
if (sleepTime > 1) {
detectedStalls.put(new Date(), sleepTime);
prune();
if (notifier != null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
notifier.cpuSleepEnded(sleepTime);
}
});
}
}
watchForSleep();
}
}, 1000);
}
private HashMap<Date,Long> detectedStalls = new HashMap<Date,Long>();
private HashMap<Date,Long> getDetectedStalls() {
return detectedStalls;
}
private void prune() {
int numberToPrune = detectedStalls.size() - 100;
if (numberToPrune > 0) {
HashMap<Date,Long> newDetectedStalls = new HashMap<Date,Long>();
ArrayList<Date> dates = new ArrayList<>(getDetectedStalls().keySet());
Collections.sort(dates);
for (int i = numberToPrune; i < detectedStalls.size(); i++) {
newDetectedStalls.put(dates.get(i), detectedStalls.get(dates.get(i)));
}
detectedStalls = newDetectedStalls;
}
}
public void logDump() {
Log.d(TAG, "Last 100 known CPU sleep incidents:");
ArrayList<Date> dates = new ArrayList<>(getDetectedStalls().keySet());
Collections.sort(dates);
for (Date date: dates) {
Log.d(TAG, ""+date+": "+getDetectedStalls().get(date));
}
}
public void setSleepEndNotifier(SleepEndNotifier notifier) {
this.notifier = notifier;
}
public interface SleepEndNotifier {
public void cpuSleepEnded(long sleepDurationMillis);
}
}

Confused. How to stop sound loop?

I am trying to make an audio app which allows sound to be projected through the speaker via the mic. I made a toggle button for this app but after click on it, it stays highlighted and I can't adjust the system volume.
So how do i break this loop?
private void setUpButton() {
final ToggleButton tb = (ToggleButton) findViewById(R.id.tb);
tb.setOnClickListener(new View.OnClickListener (){
public void onClick(View view) {
// TODO Auto-generated method stub
boolean on = ((ToggleButton) view).isChecked();
if (on){
boolean is = true;
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int buffersize = AudioRecord.getMinBufferSize(20000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 20000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC, 20000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
atrack.setPlaybackRate(20000);
byte[] buffer = new byte[buffersize];
arec.startRecording();
atrack.play();
while(is) {
arec.read(buffer, 0, buffersize);
atrack.write(buffer, 0, buffer.length);
}
}
}});
}
The toggle button is staying highlighted because you have your infinite while(is){...} loop running in the main UI thread (as per 323go's comment). The main UI thread becomes busy constantly running the infinite loop and therefore does not have the opportunity to process any more UI actions such as further button state updates etc.
Move your infinite loop into it's own thread with a message handler to handle messages from your main UI thread telling it when to start or stop etc. This will free up the main UI thread to do what it's meant to be doing ... handling the User Interface.
For example:
MainActivity
public class MainActivity {
private AudioScheduler mAudioThread;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioThread = new AudioThread();
mAudioThread.start();
final ToggleButton tb = (ToggleButton) findViewById(R.id.tb);
tb.setOnClickListener(new View.OnClickListener (){
public void onClick(View view) {
Message messageToAudioThread = Message.obtain();
boolean on = ((ToggleButton) view).isChecked();
if (on){
messageToAudioThread.what = AudioThread.PLAY;
} else {
messageToAudioThread.what = AudioThread.STOP;
}
mAudioThread.getHandler().sendMessage(messageToAudioThread);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mAudioThread.close();
}
}
AudioThread
public class AudioThread extends Thread {
// Handler message constants
public static final int PLAY = 0;
public static final int STOP = 1;
// Class variables
private boolean mRunning = false;
private boolean mPlayAudio = false;
private static class AudioThreadHandler extends Handler {
private final WeakReference<AudioThread> mThread;
public AudioThreadHandler(AudioThread thread) {
mThread = new WeakReference<AudioThread>(thread);
}
#Override
public void handleMessage(Message msg) {
AudioThread thread = mThread.get();
if (thread != null) {
switch(msg.what) {
case PLAY:
thread.mPlayAudio = true;
break;
case STOP:
thread.mPlayAudio = false;
break;
}
}
}
};
private final AudioThreadHandler mAudioThreadHandler = new AudioThreadHandler(this);
#Override
public void run() {
try {
mRunning = true;
while(mRunning) {
if (mPlayAudio) {
// Your code for handling audio recording/playback
// or whatever goes here
} else {
// If you have code you want to keep executing while
// audio is not active, stick it here
}
}
} catch (Exception e) {
Log.e("AudioThread", "Thread Loop Exception: " + e);
}
}
public void close() {
mRunning = false;
}
public Handler getHandler() {
return mAudioThreadHandler;
}
}
From your description you are wanting to record, manipulate (adjust volume etc), and output audio all in real time. For that level of audio processing you will need to understand and use threads. There are plenty of good examples and tutorials out there if you don't already have a good grasp of threads.

Android AudioRecord - Won't Initialize 2nd time

Hej, im currently trying to get AudioRecord to work. Because I need it in a bigger project. But it seems to mess up a lot.
I have been trying alot of things, so I went back to basic when I traced this bug.
I am using my Samsung Galaxy S as my debugdevice.
My problem is, first time after a reboot of my device I can initialize the AudioRecord object I create without problems.
But the second time I run it, it won't initialize the AudioRecord object.
I have tried several frequencies, fyi.
Here is my code:
package android.audiorecordtest;
import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AudioRecordTest extends Activity {
int frequency;
AudioRecord audRec;
TextView txtVw;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtVw = (TextView) findViewById(R.id.txtVw);
frequency=8000;
int bufferSize=(AudioRecord.getMinBufferSize(frequency, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT))*2;
if (bufferSize>0) {
audRec = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
int status = audRec.getState();
if (status == AudioRecord.STATE_INITIALIZED) {
txtVw.setText("Initialized" + frequency);
} else {
txtVw.setText("Not Initialized i=" + frequency);
}
}
After a few hours of looking through logcat information i found this event
02-28 10:46:37.048: DEBUG/dalvikvm(4477): GC_EXPLICIT freed 1801 objects / 98944 bytes in 97ms
02-28 10:46:37.048: VERBOSE/AudioRecord(4477): stop
Which seems to "release the native hold on the AudioRecord.
So i tried doing an override of finalize with my Audiorecord object.release(). This didnt work though.. Anyone have any idea?
I was able to reproduce your problem (on a Samsung phone). I added an onDestroy() method releasing the record:
#Override
public void onDestroy() {
super.onDestroy();
System.out.println("OnDestroy");
audRec.release();
}
After adding this, the audioRecord seems to initialize correctly every time the activity is started.
I had the same problem, usually the audRec.release() helps indeed, but if you need to stop and start several times the following code is more robust. Plus, I had an issue that the recording took place in a separate thread and Android sometimes kills threads when running for a long time. So take a look at this code, it makes sure the recording is held even when the other thread is dead and upon the following audRec.start() it stops and releases:
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
public class RecorderSingleton {
private static final int FREQUENCY = 16000;
public static RecorderSingleton instance = new RecorderSingleton();
private AudioRecord recordInstance = null;
private int bufferSize;
private RecorderSingleton() {
bufferSize = AudioRecord.getMinBufferSize(FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
}
public boolean init() {
recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
if (recordInstance.getState() == AudioRecord.STATE_UNINITIALIZED) {
return false;
}
return true;
}
public int getBufferSize() {
return bufferSize;
}
public boolean start() {
if (recordInstance != null && recordInstance.getState() != AudioRecord.STATE_UNINITIALIZED) {
if (recordInstance.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED) {
recordInstance.stop();
}
recordInstance.release();
}
if (!init()) {
return false;
}
recordInstance.startRecording();
return true;
}
public int read(short[] tempBuffer) {
if (recordInstance == null) {
return AudioRecord.ERROR_INVALID_OPERATION;
}
int ret = recordInstance.read(tempBuffer, 0, bufferSize);
return ret;
}
public void stop() {
if (recordInstance == null) {
return;
}
recordInstance.stop();
recordInstance.release();
}
}
Then if you have a recorder thread you can use it as follows:
import android.media.AudioRecord;
public class Recorder implements Runnable {
private int requiredSamples;
private int takenSamples = 0;
private boolean cancelled = false;
public void run() {
// We're important...
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
int bufferRead = 0;
int bufferSize = RecorderSingleton.instance.getBufferSize();
short[] tempBuffer = new short[bufferSize];
if (!RecorderSingleton.instance.start()) {
return;
}
try {
Log.d(RoomieConstants.LOG_TAG, "Recorder Started");
while (takenSamples < requiredSamples && !cancelled) {
bufferRead = RecorderSingleton.instance.read(tempBuffer);
if (bufferRead == AudioRecord.ERROR_INVALID_OPERATION) {
throw new IllegalStateException("read() returned AudioRecord.ERROR_INVALID_OPERATION");
} else if (bufferRead == AudioRecord.ERROR_BAD_VALUE) {
throw new IllegalStateException("read() returned AudioRecord.ERROR_BAD_VALUE");
}
takenSamples += bufferRead;
// do something with the samples ...
// ...
// ...
}
} finally {
// Close resources...
stop();
}
}
public void stop() {
RecorderSingleton.instance.stop();
}
public void cancel() {
cancelled = true;
}
}
To Answer my own question, the only way i found it doable to use AudioRecord, is to never have it as an global variable, dont know why, but it seems it won't let you release the resources of the instance correctly if you do so.
You should try to call audRec.stop() to release the resource.
My AudioRecord didn't initialize because it was static

Categories

Resources