Recording audio with device with xamarin android app.

Hello everyone!

I am making an app for my internship project, and sadly there are no programmers here with any knowledge of apps or c#.

I am trying to make an app which can record sound and put it over an existing video.
First I tried to see if it's even possible at all to record audio, so I tried the sample code given by xamarin. But sadly, even the directly copied code does not work.

(using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
using System.IO;

namespace test2
{
[Activity(Label = "test2", MainLauncher = true)]
public class Activity1 : Activity
{
MediaRecorder _recorder;
MediaPlayer _player;
Button _start;
Button _stop;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.Main);

        Button _start = FindViewById<Button>(Resource.Id.start);
        Button _stop = FindViewById<Button>(Resource.Id.stop);

        string path = "/sdcard/test.3gpp";


        _start.Click += delegate {
            _stop.Enabled = !_stop.Enabled;
            _start.Enabled = !_start.Enabled;

            _recorder.SetAudioSource(AudioSource.Mic);
            _recorder.SetOutputFormat(OutputFormat.ThreeGpp);
            _recorder.SetAudioEncoder(AudioEncoder.AmrNb);
            _recorder.SetOutputFile(path);
            _recorder.Prepare();
            _recorder.Start();
        };

        _stop.Click += delegate {
            _stop.Enabled = !_stop.Enabled;

            _recorder.Stop();
            _recorder.Reset();

            _player.SetDataSource(path);
            _player.Prepare();
            _player.Start();
        };
    }

    protected override void OnResume()
    {
        base.OnResume();

        _recorder = new MediaRecorder();
        _player = new MediaPlayer();

        _player.Completion += (sender, e) => {
            _player.Reset();
            _start.Enabled = !_start.Enabled;
        };
    }

    protected override void OnPause()
    {
        base.OnPause();

        _player.Release();
        _recorder.Release();

        _player.Dispose();
        _recorder.Dispose();
        _player = null;
        _recorder = null;
    }
}

})

I am testing on both a Oneplus5 and a Samsung galaxy s7.

Anyone else having this problem or a solution?

Sign In or Register to comment.