// Runs on a SECONDARY thread @Override public void processFrame(android.media.audiofx.Visualizer visualizer, boolean playing) { if (!lock.lockLowPriority()) return; try { if (transmitting) { // We use ignoreInput, because sampling 1024, 60 times a seconds, // is useless, as there are only 44100 or 48000 samples in one second if (ignoreInput == 0) { // WE MUST NEVER call any method from visualizer // while the player is not actually playing if (!playing) Arrays.fill(waveform, 0, 1024, (byte) 0x80); else visualizer.getWaveForm(waveform); } if (framesToSkip <= 0) { framesToSkip = framesToSkipOriginal; bt.getOutputStream() .write( waveform, 0, SimpleVisualizerJni.commonProcess(waveform, size | ignoreInput | dataType)); packetsSent++; } else { SimpleVisualizerJni.commonProcess(waveform, ignoreInput | dataType); framesToSkip--; } ignoreInput ^= IGNORE_INPUT; } int stateI = state.getAndSet(0); if (stateI != 0) { // Build and send a Player state message waveform[0] = StartOfHeading; waveform[1] = (byte) MessagePlayerState; waveform[3] = 0; int len = 0; len = writeByte(waveform, len, stateI & 3); len = writeByte(waveform, len, stateVolume); stateI = stateSongPosition; len = writeByte(waveform, len, stateI); len = writeByte(waveform, len, stateI >> 8); len = writeByte(waveform, len, stateI >> 16); len = writeByte(waveform, len, stateI >> 24); stateI = stateSongLength; len = writeByte(waveform, len, stateI); len = writeByte(waveform, len, stateI >> 8); len = writeByte(waveform, len, stateI >> 16); len = writeByte(waveform, len, stateI >> 24); waveform[2] = (byte) (len << 1); waveform[4 + len] = EndOfTransmission; bt.getOutputStream().write(waveform, 0, len + 5); packetsSent++; } } catch (IOException ex) { // Bluetooth error if (connected) MainHandler.sendMessage(this, MSG_BLUETOOTH_RXTX_ERROR); } catch (Throwable ex) { ex.printStackTrace(); } finally { lock.releaseLowPriority(); } }
public void destroy() { if (waveform != null) { version++; connected = false; transmitting = false; Player.bluetoothVisualizerState = Player.BLUETOOTH_VISUALIZER_STATE_INITIAL; lock.lockHighPriority(); try { waveform = null; if (bt != null) { bt.destroy(); bt = null; } } finally { lock.releaseHighPriority(); } if (fxVisualizer != null) { fxVisualizer.destroy(); fxVisualizer = null; } Player.stopBluetoothVisualizer(); BackgroundActivityMonitor.bluetoothEnded(); } }