我使用超声波传感器在arduino中传输数据(距离),我想创建一个哔哔声,根据距离的值改变速度。所以我在我的beginListenForData()
方法中有这个。
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;
}
当我尝试这段代码时,当距离降到70以下时会发出蜂鸣声但是当它达到60以下时会发出IllegalStateException错误,我做错了什么?
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)
是fifty.start();
答案 0 :(得分:2)
致电fifty.release()
后,您无法再次使用fifty mediaplayer
。 Release
表示mediaPlayer object
不再有效。来自文档:
在发布()之后,该对象不再可用。
如果你未满70岁,你正在释放fifty mediaplayer object
,然后它达到60岁以下的值,你再次启动它。在调用start()
之前,您必须确保在该状态下再次初始化媒体播放器。
例如:
发布媒体播放器对象:
private void releaseFifty(){
fifty.release();
fifty = null;
}
再次使用之前初始化:
private void initAndStartFifty(){
fifty = MediaPlayer.create(activity.this, R.raw.yourRecord);
fifty.start();
}