Java Code Examples for android.media.AudioTrack#PLAYSTATE_PLAYING

The following examples show how to use android.media.AudioTrack#PLAYSTATE_PLAYING . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockingAudioTrack.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static int writeToAudioTrack(AudioTrack audioTrack, byte[] bytes) {
    if (audioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
        if (DBG) Log.d(TAG, "AudioTrack not playing, restarting : " + audioTrack.hashCode());
        audioTrack.play();
    }

    int count = 0;
    while (count < bytes.length) {
        // Note that we don't take bufferCopy.mOffset into account because
        // it is guaranteed to be 0.
        int written = audioTrack.write(bytes, count, bytes.length);
        if (written <= 0) {
            break;
        }
        count += written;
    }
    return count;
}
 
Example 2
Source File: AudioTrackManagerDualNormal.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
@Override
public void resumeMusicAudioTrack() {
    // some times, initMusicAudioTrack() maybe fail
    if (mMusicAudioTrack == null) {
        reInitAudioTrack();
    }

    if ((mMusicAudioTrack != null) && (mMusicAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)) {

        try {
            mMusicAudioTrack.play();
        } catch (IllegalStateException e) {
            LogUtil.e(FTAG, "media resume failed!");
            informMusicPause();
            e.printStackTrace();
        }
    }

    if (getMusicAudioTrackFocus() == 0) {
        setMediaAudioFocusStatus(true);
    } else {
        setMediaAudioFocusStatus(false);
    }
}
 
Example 3
Source File: MediaPlayer.java    From NetEasyNews with GNU General Public License v3.0 5 votes vote down vote up
private void audioTrackWrite(byte[] audioData, int offsetInBytes, int sizeInBytes) {
  if (mAudioTrack != null && mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
    int written;
    while (sizeInBytes > 0) {
      written = sizeInBytes > mAudioTrackBufferSize ? mAudioTrackBufferSize : sizeInBytes;
      mAudioTrack.write(audioData, offsetInBytes, written);
      sizeInBytes -= written;
      offsetInBytes += written;
    }
  }
}
 
Example 4
Source File: SaiyTextToSpeech.java    From Saiy-PS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void shutdown() {

    if (audioTrack != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        switch (audioTrack.getPlayState()) {
            case AudioTrack.PLAYSTATE_PLAYING:
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "stop: PLAYSTATE_PLAYING");
                }
                audioTrack.stop(true);
                audioTrack.flush();
                audioTrack.release();
            case AudioTrack.PLAYSTATE_PAUSED:
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "stop: PLAYSTATE_PAUSED");
                }
                audioTrack.stop(true);
                audioTrack.flush();
                audioTrack.release();
            case AudioTrack.PLAYSTATE_STOPPED:
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "stop: PLAYSTATE_STOPPED");
                }
                audioTrack.flush();
                audioTrack.release();
                break;
        }
    }

    super.shutdown();
}
 
Example 5
Source File: AudioTrackManagerSingle.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public void resumeAudioTrack() {
    if ((mAudioTrack != null) && (mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)) {
        try {
            mAudioTrack.pause();
            mAudioTrack.flush();
            mAudioTrack.play();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    // get audio track focus
    getAudioTrackFocus();
}
 
Example 6
Source File: AudioOutputQueue.java    From Android-Airplay-Server with MIT License 5 votes vote down vote up
private long getNowLineTime() {
	//getPlaybackHeadPosition()
	//getNotificationMarkerPosition
	//return audioTrack.getNotificationMarkerPosition();
	//return m_line.getLongFramePosition();
	if(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING){
		long value = audioTrack.getPlaybackHeadPosition();
		return value;
	}
	else{
		LOG.warning("getNowLineTime() called while audioTrack is not on a Playing State");
		return 0;
	}
}
 
Example 7
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTTSAudioTrack(byte[] data, int offset, int size) {
    /*
     * some times, even set AudioTrack.play(), but AudioTrack.getPlayState()!=AudioTrack.PLAYSTATE_PLAYING
     */
    if (mTTSAudioTrack != null && mTTSAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
        // can not catch exception
        if (getTTSAudioFocusStatus()) {
            if (size > 0) {
                mTTSAudioTrack.write(data, offset, size);
            }
        }
    }
}
 
Example 8
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void writeMusicAudioTrack(byte[] data, int offset, int size) {
    if (mMusicAudioTrack != null && mMusicAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
        // can not catch exception
        if (getMediaAudioFocusStatus()) {
            mMusicAudioTrack.write(data, offset, size);
        }
    }
}
 
Example 9
Source File: AudioTrackManagerDualStreamType.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void pauseMusicAudioTrack() {
    // release Audio Focus
    releaseMusicAudioTrackFocus();

    if ((mMusicAudioTrack != null) && (mMusicAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING)) {
        try {
            mMusicAudioTrack.pause();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
}
 
Example 10
Source File: AudioTrackManagerDualNormal.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTTSAudioTrack(byte[] data, int offset, int size) {
    /*
     * some times, even set AudioTrack.play(), but AudioTrack.getPlayState()!=AudioTrack.PLAYSTATE_PLAYING
     */
    if (mTTSAudioTrack != null && mTTSAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
        // can not catch exception
        if (getTTSAudioFocusStatus()) {
            if (size > 0) {
                mTTSAudioTrack.write(data, offset, size);
            }
        }
    }
}
 
Example 11
Source File: AudioTrackManagerDualNormal.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
@Override
public void writeMusicAudioTrack(byte[] data, int offset, int size) {
    if (mMusicAudioTrack != null && mMusicAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
        // can not catch exception
        if (getMediaAudioFocusStatus()) {
            mMusicAudioTrack.write(data, offset, size);
        }
    }
}
 
Example 12
Source File: MediaPlayer.java    From BambooPlayer with Apache License 2.0 5 votes vote down vote up
private void audioTrackWrite(byte[] audioData, int offsetInBytes, int sizeInBytes) {
  if (mAudioTrack != null && mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
    int written;
    while (sizeInBytes > 0) {
      written = sizeInBytes > mAudioTrackBufferSize ? mAudioTrackBufferSize : sizeInBytes;
      mAudioTrack.write(audioData, offsetInBytes, written);
      sizeInBytes -= written;
      offsetInBytes += written;
    }
  }
}
 
Example 13
Source File: StreamPlayer.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Interrupt the audioStream.
 */
public void interrupt() {
  if (audioTrack != null) {
    if (audioTrack.getState() == AudioTrack.STATE_INITIALIZED
            || audioTrack.getState() == AudioTrack.PLAYSTATE_PLAYING) {
      audioTrack.pause();
    }
    audioTrack.flush();
    audioTrack.release();
  }
}
 
Example 14
Source File: AudioPlayer.java    From Android with Apache License 2.0 5 votes vote down vote up
public void stopPlayer() {
    
    if (!mIsPlayStarted) {
        return;
    }
    
    if (mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
        mAudioTrack.stop();                        
    }
    
    mAudioTrack.release();
    mIsPlayStarted = false;
       
    Log.d(TAG, "Stop audio player success !");
}
 
Example 15
Source File: MediaPlayer.java    From Vitamio with Apache License 2.0 5 votes vote down vote up
private void audioTrackWrite(byte[] audioData, int offsetInBytes, int sizeInBytes) {
  if (mAudioTrack != null && mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING) {
    int written;
    while (sizeInBytes > 0) {
      written = sizeInBytes > mAudioTrackBufferSize ? mAudioTrackBufferSize : sizeInBytes;
      mAudioTrack.write(audioData, offsetInBytes, written);
      sizeInBytes -= written;
      offsetInBytes += written;
    }
  }
}
 
Example 16
Source File: MediaPlayer.java    From react-native-android-vitamio with MIT License 4 votes vote down vote up
private void audioTrackStart() {
  if (mAudioTrack != null && mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED && mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
    mAudioTrack.play();
}
 
Example 17
Source File: MediaPlayer.java    From BambooPlayer with Apache License 2.0 4 votes vote down vote up
private void audioTrackStart() {
  if (mAudioTrack != null && mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED && mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
    mAudioTrack.play();
}
 
Example 18
Source File: TGSourceDataLine.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isRunning() {
	return (this.audioTrack != null && this.audioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING);
}
 
Example 19
Source File: MediaPlayer.java    From MyHearts with Apache License 2.0 4 votes vote down vote up
private void audioTrackStart() {
  if (mAudioTrack != null && mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED && mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
    mAudioTrack.play();
}
 
Example 20
Source File: MediaPlayer.java    From video-player with MIT License 4 votes vote down vote up
private void audioTrackStart() {
  if (mAudioTrack != null && mAudioTrack.getState() == AudioTrack.STATE_INITIALIZED && mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
    mAudioTrack.play();
}