private void playModulation() { Modulation.initEncoder(); // for play mTimer = new Timer(true); mTimerTask = new TimerTask() { public void run() { if (mIsPlaying) { if (!mIsCurrPlaying) { play(); mCurCount++; } } } }; mTimer.schedule(mTimerTask, mPlayInterval, mPlayInterval); if (Modulation.MODULATION_OPTION_LISTEN_REPLY && initRecord()) { listenAck(); } else { while (mIsPlaying) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } if (mTimer != null) { mTimer.cancel(); mTimer = null; mTimerTask = null; } try { if (mPlayerWeakRef != null && mPlayerWeakRef.get() != null) { mPlayerWeakRef.get().release(); } } catch (Exception e) { e.printStackTrace(); } Modulation.releaseEncoder(); }
private void listenAck() { Modulation.initDecoder(); // for record Modulation.setListenMode('p'); mRecordData = new short[mRecordBufSize / 2]; while (!Thread.interrupted() && mIsPlaying) { int readBytes = mRecorder.read(mRecordData, 0, mRecordBufSize / 2); if (readBytes > 0) { int ret = Modulation.process(mRecordData, readBytes); if (ret == 2) { String str = ""; byte[] result = Modulation.getResult(); try { str = new String(result, "UTF-8"); Log.d(TAG, "recorder ProcessData GetResult : " + str); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Message msg = mHandler.obtainMessage(); msg.what = Modulation.MODULATION_HANDLER_RECV_NOTIFY_ACK; mHandler.sendMessage(msg); // break; } } else { Log.d(TAG, "recorder read ret : " + readBytes); } } try { if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); mRecorder = null; } } catch (Exception e) { e.printStackTrace(); } Modulation.setListenMode('r'); Modulation.releaseDecoder(); }