Example #1
0
    private void initPlayer()
    {
        Encoding encoding = Encoding.Pcm16bit;

        bufferSize = AudioTrack.GetMinBufferSize(sampleRate, ChannelOut.Mono, encoding);
        Logging.info("Min. buffer size " + bufferSize);
        int new_buffer_size = CodecTools.getPcmFrameByteSize(sampleRate, bitRate, channels) * 100;

        if (bufferSize < new_buffer_size)
        {
            bufferSize = (int)(Math.Ceiling((decimal)new_buffer_size / bufferSize) * bufferSize);
        }
        Logging.info("Final buffer size " + bufferSize);

        // Prepare player
        AudioAttributes aa = new AudioAttributes.Builder()
                             .SetContentType(AudioContentType.Speech)
                             .SetFlags(AudioFlags.LowLatency)
                             .SetUsage(AudioUsageKind.VoiceCommunication)
                             .Build();

        AudioFormat af = new AudioFormat.Builder()
                         .SetSampleRate(sampleRate)
                         .SetChannelMask(ChannelOut.Mono)
                         .SetEncoding(encoding)
                         .Build();

        audioPlayer = new AudioTrack(aa, af, bufferSize, AudioTrackMode.Stream, 0);

        MainActivity.Instance.VolumeControlStream = Stream.VoiceCall;

        audioPlayer.Play();
    }
Example #2
0
    private void initRecorder()
    {
        audioRecorder = new AVAudioEngine();
        NSError error = new NSError();

        if (!AVAudioSession.SharedInstance().SetPreferredSampleRate(sampleRate, out error))
        {
            throw new Exception("Error setting preffered sample rate for recorder: " + error);
        }
        AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.PlayAndRecord, AVAudioSessionCategoryOptions.InterruptSpokenAudioAndMixWithOthers);
        AVAudioSession.SharedInstance().SetActive(true);
        AVAudioFormat recording_format = new AVAudioFormat(AVAudioCommonFormat.PCMInt16, sampleRate, (uint)channels, false);
        uint          buffer_size      = (uint)CodecTools.getPcmFrameByteSize(sampleRate, bitRate, channels) * 1000;

        audioRecorder.InputNode.InstallTapOnBus(0, buffer_size, recording_format, onDataAvailable);
        audioRecorder.Prepare();
        if (!audioRecorder.StartAndReturnError(out error))
        {
            throw new Exception("Error starting recording audio engine: " + error);
        }
    }