Java Code Examples for android.media.AudioManager#AUDIO_SESSION_ID_GENERATE

The following examples show how to use android.media.AudioManager#AUDIO_SESSION_ID_GENERATE . 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: AndroidAudioPlayer.java    From Android-Guitar-Tuner with Apache License 2.0 10 votes vote down vote up
public AndroidAudioPlayer(final AudioConfig audioConfig) {
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setLegacyStreamType(AudioManager.STREAM_MUSIC)
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();

    AudioFormat audioFormat = new AudioFormat.Builder()
            .setChannelMask(audioConfig.getOutputChannel())
            .setEncoding(audioConfig.getOutputFormat())
            .setSampleRate(audioConfig.getSampleRate())
            .build();

    audioTrack = new AudioTrack(audioAttributes,
            audioFormat,
            audioConfig.getOutputBufferSize(),
            AudioTrack.MODE_STATIC,
            AudioManager.AUDIO_SESSION_ID_GENERATE);

    outputByteCount = audioConfig.getOutputFormatByteCount();
}
 
Example 2
Source File: WebRtcAudioTrack.java    From webrtc_android with MIT License 6 votes vote down vote up
@TargetApi(21)
private static AudioTrack createAudioTrackOnLollipopOrHigher(
    int sampleRateInHz, int channelConfig, int bufferSizeInBytes) {
  Logging.d(TAG, "createAudioTrackOnLollipopOrHigher");
  // TODO(henrika): use setPerformanceMode(int) with PERFORMANCE_MODE_LOW_LATENCY to control
  // performance when Android O is supported. Add some logging in the mean time.
  final int nativeOutputSampleRate =
      AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_VOICE_CALL);
  Logging.d(TAG, "nativeOutputSampleRate: " + nativeOutputSampleRate);
  if (sampleRateInHz != nativeOutputSampleRate) {
    Logging.w(TAG, "Unable to use fast mode since requested sample rate is not native");
  }
  // Create an audio track where the audio usage is for VoIP and the content type is speech.
  return new AudioTrack(new AudioAttributes.Builder()
                            .setUsage(DEFAULT_USAGE)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                            .build(),
      new AudioFormat.Builder()
          .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
          .setSampleRate(sampleRateInHz)
          .setChannelMask(channelConfig)
          .build(),
      bufferSizeInBytes, AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE);
}
 
Example 3
Source File: DefaultAudioSink.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@TargetApi(21)
private AudioTrack createAudioTrackV21() {
  android.media.AudioAttributes attributes;
  if (tunneling) {
    attributes = new android.media.AudioAttributes.Builder()
        .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
        .setFlags(android.media.AudioAttributes.FLAG_HW_AV_SYNC)
        .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
        .build();
  } else {
    attributes = audioAttributes.getAudioAttributesV21();
  }
  AudioFormat format =
      new AudioFormat.Builder()
          .setChannelMask(outputChannelConfig)
          .setEncoding(outputEncoding)
          .setSampleRate(outputSampleRate)
          .build();
  int audioSessionId = this.audioSessionId != C.AUDIO_SESSION_ID_UNSET ? this.audioSessionId
      : AudioManager.AUDIO_SESSION_ID_GENERATE;
  return new AudioTrack(attributes, format, bufferSize, MODE_STREAM, audioSessionId);
}
 
Example 4
Source File: DefaultAudioSink.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@TargetApi(21)
private AudioTrack createAudioTrackV21() {
  android.media.AudioAttributes attributes;
  if (tunneling) {
    attributes = new android.media.AudioAttributes.Builder()
        .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
        .setFlags(android.media.AudioAttributes.FLAG_HW_AV_SYNC)
        .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
        .build();
  } else {
    attributes = audioAttributes.getAudioAttributesV21();
  }
  AudioFormat format =
      new AudioFormat.Builder()
          .setChannelMask(outputChannelConfig)
          .setEncoding(outputEncoding)
          .setSampleRate(outputSampleRate)
          .build();
  int audioSessionId = this.audioSessionId != C.AUDIO_SESSION_ID_UNSET ? this.audioSessionId
      : AudioManager.AUDIO_SESSION_ID_GENERATE;
  return new AudioTrack(attributes, format, bufferSize, MODE_STREAM, audioSessionId);
}
 
Example 5
Source File: DefaultAudioSink.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@TargetApi(21)
private AudioTrack createAudioTrackV21(
    boolean tunneling, AudioAttributes audioAttributes, int audioSessionId) {
  android.media.AudioAttributes attributes;
  if (tunneling) {
    attributes =
        new android.media.AudioAttributes.Builder()
            .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
            .setFlags(android.media.AudioAttributes.FLAG_HW_AV_SYNC)
            .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
            .build();
  } else {
    attributes = audioAttributes.getAudioAttributesV21();
  }
  AudioFormat format =
      new AudioFormat.Builder()
          .setChannelMask(outputChannelConfig)
          .setEncoding(outputEncoding)
          .setSampleRate(outputSampleRate)
          .build();
  return new AudioTrack(
      attributes,
      format,
      bufferSize,
      MODE_STREAM,
      audioSessionId != C.AUDIO_SESSION_ID_UNSET
          ? audioSessionId
          : AudioManager.AUDIO_SESSION_ID_GENERATE);
}
 
Example 6
Source File: WebRtcAudioTrack.java    From webrtc_android with MIT License 5 votes vote down vote up
@TargetApi(21)
private static AudioTrack createAudioTrackOnLollipopOrHigher(
    int sampleRateInHz, int channelConfig, int bufferSizeInBytes) {
  Logging.d(TAG, "createAudioTrackOnLollipopOrHigher");
  // TODO(henrika): use setPerformanceMode(int) with PERFORMANCE_MODE_LOW_LATENCY to control
  // performance when Android O is supported. Add some logging in the mean time.
  final int nativeOutputSampleRate =
      AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_VOICE_CALL);
  Logging.d(TAG, "nativeOutputSampleRate: " + nativeOutputSampleRate);
  if (sampleRateInHz != nativeOutputSampleRate) {
    Logging.w(TAG, "Unable to use fast mode since requested sample rate is not native");
  }
  if (usageAttribute != DEFAULT_USAGE) {
    Logging.w(TAG, "A non default usage attribute is used: " + usageAttribute);
  }
  // Create an audio track where the audio usage is for VoIP and the content type is speech.
  return new AudioTrack(
      new AudioAttributes.Builder()
          .setUsage(usageAttribute)
          .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
      .build(),
      new AudioFormat.Builder()
        .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
        .setSampleRate(sampleRateInHz)
        .setChannelMask(channelConfig)
        .build(),
      bufferSizeInBytes,
      AudioTrack.MODE_STREAM,
      AudioManager.AUDIO_SESSION_ID_GENERATE);
}
 
Example 7
Source File: TextToSpeechService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** Create AudioOutputParams with default values */
AudioOutputParams() {
    mSessionId = AudioManager.AUDIO_SESSION_ID_GENERATE;
    mVolume = Engine.DEFAULT_VOLUME;
    mPan = Engine.DEFAULT_PAN;
    mAudioAttributes = null;
}
 
Example 8
Source File: DefaultAudioSink.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(21)
private AudioTrack createAudioTrackV21(
    boolean tunneling, AudioAttributes audioAttributes, int audioSessionId) {
  android.media.AudioAttributes attributes;
  if (tunneling) {
    attributes =
        new android.media.AudioAttributes.Builder()
            .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
            .setFlags(android.media.AudioAttributes.FLAG_HW_AV_SYNC)
            .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
            .build();
  } else {
    attributes = audioAttributes.getAudioAttributesV21();
  }
  AudioFormat format =
      new AudioFormat.Builder()
          .setChannelMask(outputChannelConfig)
          .setEncoding(outputEncoding)
          .setSampleRate(outputSampleRate)
          .build();
  return new AudioTrack(
      attributes,
      format,
      bufferSize,
      MODE_STREAM,
      audioSessionId != C.AUDIO_SESSION_ID_UNSET
          ? audioSessionId
          : AudioManager.AUDIO_SESSION_ID_GENERATE);
}
 
Example 9
Source File: DefaultAudioSink.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@TargetApi(21)
private AudioTrack createAudioTrackV21(
    boolean tunneling, AudioAttributes audioAttributes, int audioSessionId) {
  android.media.AudioAttributes attributes;
  if (tunneling) {
    attributes =
        new android.media.AudioAttributes.Builder()
            .setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
            .setFlags(android.media.AudioAttributes.FLAG_HW_AV_SYNC)
            .setUsage(android.media.AudioAttributes.USAGE_MEDIA)
            .build();
  } else {
    attributes = audioAttributes.getAudioAttributesV21();
  }
  AudioFormat format =
      new AudioFormat.Builder()
          .setChannelMask(outputChannelConfig)
          .setEncoding(outputEncoding)
          .setSampleRate(outputSampleRate)
          .build();
  return new AudioTrack(
      attributes,
      format,
      bufferSize,
      MODE_STREAM,
      audioSessionId != C.AUDIO_SESSION_ID_UNSET
          ? audioSessionId
          : AudioManager.AUDIO_SESSION_ID_GENERATE);
}