public void SetupAudio(int sampleRate, int channels, byte[] esdsData) { _audioTrack = new AudioTrack( new AudioAttributes.Builder() .SetUsage(AudioUsageKind.Media) .SetContentType(AudioContentType.Music) .SetFlags(AudioFlags.LowLatency) .Build(), new Android.Media.AudioFormat.Builder() .SetEncoding(Encoding.Pcm16bit) .SetSampleRate(44100) .SetChannelMask(ChannelOut.Stereo) .Build(), 4096, AudioTrackMode.Stream, AudioManager.AudioSessionIdGenerate); MediaFormat audioFormat = MediaFormat.CreateAudioFormat( mime: MediaFormat.MimetypeAudioAac, sampleRate: sampleRate, channelCount: channels); audioFormat.SetInteger(MediaFormat.KeyIsAdts, 0); audioFormat.SetInteger(MediaFormat.KeyAacProfile, (int)MediaCodecProfileType.Aacobjectlc); _audioCodec = MediaCodec.CreateDecoderByType( MediaFormat.MimetypeAudioAac); // TODO: Remove hardcoding byte profile = (byte)MediaCodecProfileType.Aacobjectlc; byte sampleIndex = AacAdtsAssembler.GetSamplingFrequencyIndex(sampleRate); byte[] csd0 = new byte[2]; csd0[0] = (byte)(((byte)profile << 3) | (sampleIndex >> 1)); csd0[1] = (byte)((byte)((sampleIndex << 7) & 0x80) | (channels << 3)); esdsData = csd0; audioFormat.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(esdsData)); _audioCodec.SetCallback(this); _audioCodec.Configure( format: audioFormat, surface: null, crypto: null, flags: MediaCodecConfigFlags.None); _audioCodec.Start(); _audioTrack.Play(); }
public static MediaFormat GetMediaFormat(string mimeType, int sampleRate, int channels) { var format = MediaFormat.CreateAudioFormat( mime: MediaFormat.MimetypeAudioAac, sampleRate: sampleRate, channelCount: channels); format.SetInteger(MediaFormat.KeyIsAdts, 0); format.SetInteger(MediaFormat.KeyAacProfile, (int)MediaCodecProfileType.Aacobjectlc); byte profile = (byte)MediaCodecProfileType.Aacobjectlc; byte sampleIndex = AacAdtsAssembler.GetSamplingFrequencyIndex(sampleRate); byte[] csd0 = new byte[2]; csd0[0] = (byte)(((byte)profile << 3) | (sampleIndex >> 1)); csd0[1] = (byte)((byte)((sampleIndex << 7) & 0x80) | (channels << 3)); format.SetByteBuffer("csd-0", Java.Nio.ByteBuffer.Wrap(csd0)); return(format); }