private void initEncoder(string codec) { MediaFormat format = new MediaFormat(); switch (codec) { case "amrnb": audioEncoder = MediaCodec.CreateEncoderByType(MediaFormat.MimetypeAudioAmrNb); format.SetString(MediaFormat.KeyMime, MediaFormat.MimetypeAudioAmrNb); format.SetInteger(MediaFormat.KeySampleRate, 8000); format.SetInteger(MediaFormat.KeyBitRate, 7950); break; case "amrwb": audioEncoder = MediaCodec.CreateEncoderByType(MediaFormat.MimetypeAudioAmrWb); format.SetString(MediaFormat.KeyMime, MediaFormat.MimetypeAudioAmrWb); format.SetInteger(MediaFormat.KeySampleRate, 16000); format.SetInteger(MediaFormat.KeyBitRate, 18250); break; default: throw new Exception("Unknown recorder codec selected " + codec); } format.SetInteger(MediaFormat.KeyChannelCount, 1); format.SetInteger(MediaFormat.KeyMaxInputSize, bufferSize); audioEncoder.SetCallback(this); audioEncoder.Configure(format, null, null, MediaCodecConfigFlags.Encode); audioEncoder.Start(); }
//@Override //@SuppressLint({ "InlinedApi", "NewApi" }) protected override void encodeWithMediaCodec() { int bufferSize = AudioRecord.GetMinBufferSize(mQuality.samplingRate, ChannelIn.Mono, Encoding.Pcm16bit) * 2; ((AACLATMPacketizer)mPacketizer).setSamplingRate(mQuality.samplingRate); mAudioRecord = new AudioRecord(Android.Media.AudioSource.Mic, mQuality.samplingRate, Android.Media.ChannelIn.Mono, Android.Media.Encoding.Pcm16bit, bufferSize); mMediaCodec = MediaCodec.CreateEncoderByType("audio/mp4a-latm"); MediaFormat format = new MediaFormat(); format.SetString(MediaFormat.KeyMime, "audio/mp4a-latm"); format.SetInteger(MediaFormat.KeyBitRate, mQuality.bitRate); format.SetInteger(MediaFormat.KeyChannelCount, 1); format.SetInteger(MediaFormat.KeySampleRate, mQuality.samplingRate); format.SetInteger(MediaFormat.KeyAacProfile, (int)MediaCodecInfo.CodecProfileLevel.AACObjectLC); format.SetInteger(MediaFormat.KeyMaxInputSize, bufferSize); mMediaCodec.Configure(format, null, null, MediaCodecConfigFlags.Encode); mAudioRecord.StartRecording(); mMediaCodec.Start(); MediaCodecInputStream inputStream = new MediaCodecInputStream(mMediaCodec); Java.Nio.ByteBuffer[] inputBuffers = mMediaCodec.GetInputBuffers(); mThread = new Thread(this); mThread.Start(); // The packetizer encapsulates this stream in an RTP stream and send it over the network mPacketizer.setInputStream(inputStream); mPacketizer.start(); mStreaming = true; }
private void initHwDecoder(string codec) { MediaFormat format = new MediaFormat(); string mime_type = null; switch (codec) { case "amrnb": mime_type = MediaFormat.MimetypeAudioAmrNb; format.SetInteger(MediaFormat.KeySampleRate, 8000); format.SetInteger(MediaFormat.KeyBitRate, 7950); break; case "amrwb": mime_type = MediaFormat.MimetypeAudioAmrWb; format.SetInteger(MediaFormat.KeySampleRate, 16000); format.SetInteger(MediaFormat.KeyBitRate, 18250); break; } if (mime_type != null) { format.SetString(MediaFormat.KeyMime, mime_type); format.SetInteger(MediaFormat.KeyChannelCount, 1); format.SetInteger(MediaFormat.KeyMaxInputSize, bufferSize); format.SetInteger(MediaFormat.KeyLatency, 1); format.SetInteger(MediaFormat.KeyPriority, 0); audioDecoder = new HwDecoder(mime_type, format, this); audioDecoder.start(); } }