Java Code Examples for android.media.AudioTrack#ERROR_BAD_VALUE

The following examples show how to use android.media.AudioTrack#ERROR_BAD_VALUE . 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: 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 2
Source File: StreamPlayer.java    From android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize AudioTrack by getting buffersize
 *
 * @param sampleRate the sample rate for the audio to be played
 */
private void initPlayer(int sampleRate) {
  synchronized (this) {
    int bufferSize = AudioTrack.getMinBufferSize(
            sampleRate,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    if (bufferSize == AudioTrack.ERROR_BAD_VALUE) {
      throw new RuntimeException("Could not determine buffer size for audio");
    }

    audioTrack = new AudioTrack(
            AudioManager.STREAM_MUSIC,
            sampleRate,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT,
            bufferSize,
            AudioTrack.MODE_STREAM
    );
    audioTrack.play();
  }
}
 
Example 3
Source File: StreamAudioPlayer.java    From RxAndroidAudio with MIT License 6 votes vote down vote up
@WorkerThread
public synchronized boolean play(byte[] data, int size) {
    if (mAudioTrack != null) {
        try {
            int ret = mAudioTrack.write(data, 0, size);
            switch (ret) {
                case AudioTrack.ERROR_INVALID_OPERATION:
                    Log.w(TAG, "play fail: ERROR_INVALID_OPERATION");
                    return false;
                case AudioTrack.ERROR_BAD_VALUE:
                    Log.w(TAG, "play fail: ERROR_BAD_VALUE");
                    return false;
                case AudioManager.ERROR_DEAD_OBJECT:
                    Log.w(TAG, "play fail: ERROR_DEAD_OBJECT");
                    return false;
                default:
                    return true;
            }
        } catch (IllegalStateException e) {
            Log.w(TAG, "play fail: " + e.getMessage());
            return false;
        }
    }
    Log.w(TAG, "play fail: null mAudioTrack");
    return false;
}
 
Example 4
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 5
Source File: AudioTrackManager.java    From TikTok with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    try {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
        byte[] tempBuffer = new byte[bufferSize];
        int readCount = 0;
        while (true) {
            if(!isLoop && dis.available() <= 0){
                break;
            }
            if(dis.available() <= 0){
                dis.reset();
            }
            readCount= dis.read(tempBuffer);
            if (readCount == AudioTrack.ERROR_INVALID_OPERATION || readCount == AudioTrack.ERROR_BAD_VALUE) {
                continue;
            }
            if (readCount != 0 && readCount != -1) {
                audioTrack.play();
                audioTrack.write(tempBuffer, 0, readCount);
            }
        }
        stopPlay();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: AudioPlayer.java    From connectivity-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean validSize(int size) {
  return size != AudioTrack.ERROR && size != AudioTrack.ERROR_BAD_VALUE;
}
 
Example 7
Source File: AudioPlayer.java    From react-native-google-nearby-connection with MIT License 4 votes vote down vote up
@Override
protected boolean validSize(int size) {
	return size != AudioTrack.ERROR && size != AudioTrack.ERROR_BAD_VALUE;
}