@Override public void run() { try { toRelease.flush(); toRelease.release(); } finally { releasingConditionVariable.open(); } } }.start();
/** * Stop player */ private void stopTtsPlayer() { if (audioTrack != null && audioTrack.getState() != AudioTrack.STATE_UNINITIALIZED ) { // IMPORTANT: NOT use stop() // For an immediate stop, use pause(), followed by flush() to discard audio data that hasn't been played back yet. audioTrack.pause(); audioTrack.flush(); } }
@Override public void run() { try { toRelease.flush(); toRelease.release(); } finally { releasingConditionVariable.open(); } } }.start();
/* Create AudioTrack instance */ AudioTrack mAudioTrack = null; int minSize = AudioTrack.getMinBufferSize( 16000, AudioFormat.CHANNEL_OUT_MONO, // or set AudioFormat.CHANNEL_OUT_STEREO AudioFormat.ENCODING_PCM_16BIT); if (mAudioTrack == null) { mAudioTrack = new AudioTrack( AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_OUT_MONO, // or set AudioFormat.CHANNEL_OUT_STEREO AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM); } /* Create AudioTrack instance */ /* Stop and release AudioTrack instance */ mAudioTrack.flush(); mAudioTrack.stop(); mAudioTrack.release(); mAudioTrack = null; /* Stop and release AudioTrack instance */
@Override protected void onDestroy() { super.onDestroy(); usbMidiDriver.close(); if (timer != null) { try { timer.cancel(); timer.purge(); } catch (Throwable t) { // do nothing } finally { timer = null; } } if (audioTrack != null) { try { audioTrack.stop(); audioTrack.flush(); audioTrack.release(); } catch (Throwable t) { // do nothing } finally { audioTrack = null; } } }
mAudioTrack.flush(); mAudioTrack.play();
@Override protected void onDestroy() { super.onDestroy(); if (timer != null) { try { timer.cancel(); timer.purge(); } catch (Throwable t) { // do nothing } finally { timer = null; } } if (audioTrack != null) { try { audioTrack.stop(); audioTrack.flush(); audioTrack.release(); } catch (Throwable t) { // do nothing } finally { audioTrack = null; } } }
@Override protected void onDestroy() { super.onDestroy(); if (timer != null) { try { timer.cancel(); timer.purge(); } catch (Throwable t) { // do nothing } finally { timer = null; } } if (audioTrack != null) { try { audioTrack.stop(); audioTrack.flush(); audioTrack.release(); } catch (Throwable t) { // do nothing } finally { audioTrack = null; } } }
public void stop() { if (isPlaying() || isPaused()) { mKeepPlaying = false; mAudioTrack.pause(); // pause() stops the playback immediately. mAudioTrack.stop(); // Unblock mAudioTrack.write() to avoid deadlocks. if (mPlayThread != null) { try { mPlayThread.join(); } catch (InterruptedException e) { } mPlayThread = null; } mAudioTrack.flush(); // just in case... } }
private void relaxResources(Boolean release) { if (codec != null) { if (release) { codec.stop(); codec.release(); codec = null; } } if (audioTrack != null) { if (!doStop) audioTrack.flush(); audioTrack.release(); audioTrack = null; } }
mAudioTrack.pause(); mLatencyTestPipe.flush(); mAudioTrack.flush();
audioTrack.flush();
@Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { AudioTrack audioTrack = mAudioTrack; if (audioTrack != null) { audioTrack.setStereoVolume(1.0f, 1.0f); if (audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PAUSED) { audioTrack.flush(); audioTrack.play(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { AudioTrack audioTrack = mAudioTrack; if (audioTrack != null) { if (audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) { audioTrack.pause(); } } } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { AudioTrack audioTrack = mAudioTrack; if (audioTrack != null) { audioTrack.setStereoVolume(0.5f, 0.5f); } } } };
private static void beep() { if(audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) { // On the initial play, we play including the attenuated lead-in // After that, we periodically feed in full periods of wave to get a continuous beep. audioTrack.flush(); currentlyBeeping = true; // Call back every time this many frames have been played, so we know we can // enqueue another bit of audio data. audioTrack.setPositionNotificationPeriod(body); audioTrack.setPlaybackHeadPosition(0); audioTrack.write(sampHead, 0, sampHead.length); // Playback will not start if we haven't filled up minBuffer bytes. // The exact number is device-dependent! // Prime the buffer with enough full waveform periods. audioTrack.write(sampBody, 0, sampBody.length); audioTrack.write(sampBody, 0, sampBody.length); audioTrack.play(); } }
@Override public Object play (final InputStream ais, final StreamInfo streamInfo, final Object stopMonitor, final int skipMilliSeconds) throws PlayObjectException { final int channelConf = this.getChannelConfiguration (streamInfo); final int frameLength; try { frameLength = findFrameLength (ais, streamInfo); } catch (IOException ioe) { throw new PlayObjectException (new SoundTransformException (PlaySoundErrorCode.COULD_NOT_PLAY_SOUND, ioe)); } final AudioTrack audioTrack = new AudioTrack (AudioManager.STREAM_MUSIC, (int) streamInfo.getSampleRate (), channelConf, streamInfo.getSampleSize () == AndroidPlayObjectProcessor.TWO ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT, frameLength * streamInfo.getSampleSize (), AudioTrack.MODE_STATIC); final byte [] baSoundByteArray = new byte [frameLength * streamInfo.getSampleSize ()]; try { final int byteArraySize = ais.read (baSoundByteArray); this.log (new LogEvent (AndroidPlaySoundProcessorEventCode.READ_BYTEARRAY_SIZE, byteArraySize)); } catch (final IOException e1) { throw new PlayObjectException (new SoundTransformException (PlaySoundErrorCode.COULD_NOT_PLAY_SOUND, e1)); } audioTrack.write (baSoundByteArray, 0, baSoundByteArray.length); audioTrack.flush (); audioTrack.setPlaybackHeadPosition ((int) (streamInfo.getSampleRate () * 1.0 * skipMilliSeconds / 1000.0)); audioTrack.play (); final Thread soundMonitorThread = this.getSoundMonitorThread (stopMonitor, audioTrack); final Thread playFrameMonitorThread = this.getPlayFrameMonitorThread (stopMonitor, audioTrack); playFrameMonitorThread.start (); if (soundMonitorThread != null) { soundMonitorThread.start (); } return playFrameMonitorThread; }