Reputation: 578
i am streaming data (distance) in arduino with an ultrasonic sensor and i want to create a beeping sound that will change its speed depending on the value of the distance.so i have this in my beginListenForData()
method.
public void beginListenForData() {
if (Looper.myLooper() == null) {
Looper.prepare();
}
final Handler handler1 = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted() && !stopWorker) {
try {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler1.post(new Runnable() {
public void run() {
incomingData = data.trim();
distance = Integer.parseInt(incomingData);
lastDist = distance;
tvResults.setText(incomingData);
/*if (distance<=80 && lastDist > 80) {
swipescreen.release();
warning.start();
warning.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
warning.release();
}
});
entered = true;
}*/
if (distance <= 70 && distance > 60) {
fifty.release();
sixty.start();
sixty.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
sixty.start();
}
});
}
if (distance <= 60 && distance > 50) {
sixty.release();
forty.release();
fifty.start();
fifty.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
fifty.start();
}
});
}
/*
if (distance <= 50 && distance > 40) {
fifty.release();
forty.start();
thirty.release();
}
if (distance <= 40 && distance > 30) {
forty.release();
thirty.start();
twenty.release();
}
if (distance <= 30 && distance > 20) {
fifteen.release();
twenty.start();
thirty.release();
}
if (distance <= 20 && distance > 10) {
ten.release();
fifteen.start();
twenty.release();
}
if (distance <= 10 && distance > 5) {
five.release();
ten.start();
fifteen.release();
}
if (distance <= 5) {
five.start();
ten.release();
}*/
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch(NullPointerException e) {}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
//return incomingData;
}
when i try this code, the beep will start when the distance drops to below 70 but when it reaches below 60 it will give an IllegalStateException error, what am i doing wrong?
java.lang.IllegalStateException
at android.media.MediaPlayer.getCurrentPosition(Native Method)
at android.media.MediaPlayer.notifyActionMetaChange(MediaPlayer.java:2734)
at android.media.MediaPlayer.start(MediaPlayer.java:1079)
at com.ardudroid.projects.ultrasoundbelt.MainActivity$38$1.run(MainActivity.java:1476)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5071)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
(MainActivity.java:1476)
is this fifty.start();
Upvotes: 0
Views: 2197
Reputation: 9870
After calling fifty.release()
, You cannot use the fifty mediaplayer
again. Release
means, the mediaPlayer object
is not longer valid. From the docs:
After release(), the object is no longer available.
You are releasing the fifty mediaplayer object
if You are under 70, then it reaches the value under 60 and You start it again. You have to be sure to initialize the media player again in that state before calling start()
.
for example:
release the media player object:
private void releaseFifty(){
fifty.release();
fifty = null;
}
init before using it again:
private void initAndStartFifty(){
fifty = MediaPlayer.create(activity.this, R.raw.yourRecord);
fifty.start();
}
Upvotes: 2