Java Code Examples for android.media.AudioTrack#STATE_UNINITIALIZED

The following examples show how to use android.media.AudioTrack#STATE_UNINITIALIZED . 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: AudioTrackPlayerImpl.java    From dcs-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() {
    getAudioTrackCurrentPosition();
    mCurrentState = PlayState.STOPPED;
    if (writeWorkThread != null) {
        writeWorkThread.stopWrite();
    }
    try {
        Log.d(TAG, "stop-PlayState:" + mAudioTrack.getPlayState());
        if (mAudioTrack != null && mAudioTrack.getPlayState() != AudioTrack.STATE_UNINITIALIZED) {
            mAudioTrack.pause();
            mAudioTrack.flush();
            Log.d(TAG, "stop-ok");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "stop()", e);
    }
    fireStopped();
}
 
Example 2
Source File: AudioTrackPlayerImpl.java    From dcs-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public void release() {
    mCurrentState = PlayState.IDLE;
    if (writeWorkThread != null) {
        writeWorkThread.stopWrite();
    }
    try {
        Log.d(TAG, "release-PlayState:" + mAudioTrack.getPlayState());
        if (mAudioTrack != null && mAudioTrack.getPlayState() != AudioTrack.STATE_UNINITIALIZED) {
            mAudioTrack.pause();
            mAudioTrack.flush();
            mAudioTrack.stop();
            mAudioTrack.release();
            Log.d(TAG, "release-ok");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "release()", e);
    }
    fireOnRelease();
    mediaPlayerListeners.clear();
    handlerMain.removeCallbacksAndMessages(null);
}
 
Example 3
Source File: AudioPlayer.java    From EvilsLive with MIT License 6 votes vote down vote up
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
    
    if (mIsPlayStarted) {
        Log.e(TAG, "Player already started !");
        return false;
    }
    
    mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
    if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
        Log.e(TAG, "Invalid parameter !");
        return false;
    }
    Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
    
    mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
    if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
        Log.e(TAG, "AudioTrack initialize fail !");
        return false;
    }            
    
    mIsPlayStarted = true;
    
    Log.d(TAG, "Start audio player success !");
    
    return true;
}
 
Example 4
Source File: StreamPlayer.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Play the given InputStream. The stream must be a PCM audio format with a sample rate of 22050.
 *
 * @param stream the stream derived from a PCM audio source
 */
public void playStream(InputStream stream) {
  try {
    byte[] data = convertStreamToByteArray(stream);
    int headSize = 44, metaDataSize = 48;
    int destPos = headSize + metaDataSize;
    int rawLength = data.length - destPos;
    byte[] d = new byte[rawLength];
    System.arraycopy(data, destPos, d, 0, rawLength);
    initPlayer(DEFAULT_SAMPLE_RATE);
    audioTrack.write(d, 0, d.length);
    stream.close();
    if (audioTrack != null && audioTrack.getState() != AudioTrack.STATE_UNINITIALIZED) {
      audioTrack.release();
    }
  } catch (IOException e2) {
    Log.e(TAG, e2.getMessage());
  }
}
 
Example 5
Source File: StreamPlayer.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Play the given InputStream. The stream must be a PCM audio format.
 *
 * @param stream the stream derived from a PCM audio source
 * @param sampleRate the sample rate for the provided stream
 */
public void playStream(InputStream stream, int sampleRate) {
  try {
    byte[] data = convertStreamToByteArray(stream);
    int headSize = 44, metaDataSize = 48;
    int destPos = headSize + metaDataSize;
    int rawLength = data.length - destPos;
    byte[] d = new byte[rawLength];
    System.arraycopy(data, destPos, d, 0, rawLength);
    initPlayer(sampleRate);
    audioTrack.write(d, 0, d.length);
    stream.close();
    if (audioTrack != null && audioTrack.getState() != AudioTrack.STATE_UNINITIALIZED) {
      audioTrack.release();
    }
  } catch (IOException e2) {
    Log.e(TAG, e2.getMessage());
  }
}
 
Example 6
Source File: AudioPlayer.java    From Android with Apache License 2.0 6 votes vote down vote up
public boolean startPlayer(int streamType, int sampleRateInHz, int channelConfig, int audioFormat) {
    
    if (mIsPlayStarted) {
        Log.e(TAG, "Player already started !");
        return false;
    }
    
    mMinBufferSize = AudioTrack.getMinBufferSize(sampleRateInHz,channelConfig,audioFormat);
    if (mMinBufferSize == AudioTrack.ERROR_BAD_VALUE) {
        Log.e(TAG, "Invalid parameter !");
        return false;
    }
    Log.d(TAG , "getMinBufferSize = "+mMinBufferSize+" bytes !");
    
    mAudioTrack = new AudioTrack(streamType,sampleRateInHz,channelConfig,audioFormat,mMinBufferSize,DEFAULT_PLAY_MODE);
    if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED) {
        Log.e(TAG, "AudioTrack initialize fail !");
        return false;
    }            
    
    mIsPlayStarted = true;
    
    Log.d(TAG, "Start audio player success !");
    
    return true;
}
 
Example 7
Source File: MediaMoviePlayer.java    From libcommon with Apache License 2.0 5 votes vote down vote up
protected void internal_stop_audio() {
	if (DEBUG) Log.v(TAG, "internal_stop_audio:");
   	if (mAudioTrack != null) {
   		if (mAudioTrack.getState() != AudioTrack.STATE_UNINITIALIZED)
   			mAudioTrack.stop();
   		mAudioTrack.release();
   		mAudioTrack = null;
   	}
	mAudioOutTempBuf = null;
}
 
Example 8
Source File: TTSUtility.java    From speech-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
    * 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();
       }
}
 
Example 9
Source File: TTSUtility.java    From speech-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
	
	HttpResponse post;
	try {
		post = createPost(server, username, password, token, learningOptOut, content, voice, codec);
        InputStream is = post.getEntity().getContent();

		byte[] data = null;
		if(codec == CODEC_WAV) {
			data = analyzeWavData(is);
		}
		else if(codec == CODEC_OPUS){
			data = analyzeOpusData(is);
		}
              initPlayer();
              audioTrack.write(data, 0, data.length);
              is.close();

	} catch (Exception e) {
		e.printStackTrace();
	} finally {
              Log.i(TAG, "Stopping audioTrack...");
		if (audioTrack != null && audioTrack.getState() != AudioTrack.STATE_UNINITIALIZED) {
			audioTrack.release();
		}
	}
}
 
Example 10
Source File: MediaMoviePlayer.java    From AudioVideoPlayerSample with Apache License 2.0 5 votes vote down vote up
protected void internalStopAudio() {
	if (DEBUG) Log.v(TAG, "internalStopAudio:");
   	if (mAudioTrack != null) {
   		if (mAudioTrack.getState() != AudioTrack.STATE_UNINITIALIZED)
   			mAudioTrack.stop();
   		mAudioTrack.release();
   		mAudioTrack = null;
   	}
	mAudioOutTempBuf = null;
}
 
Example 11
Source File: Aout.java    From VLC-Simple-Player-Android with MIT License 5 votes vote down vote up
public void playBuffer(byte[] audioData, int bufferSize) {
    if (mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED)
        return;
    if (mAudioTrack.write(audioData, 0, bufferSize) != bufferSize) {
        Log.w(TAG, "Could not write all the samples to the audio device");
    }
    mAudioTrack.play();
}