Skip to content

Instantly share code, notes, and snippets.

@Xonshiz Xonshiz/Main.axml
Created Mar 15, 2018

Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recording" />
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Stop Recording" />
</LinearLayout>
using Android.App;
using Android.Widget;
using Android.OS;
using System.IO;
using Android.Media;
namespace VoicRec2
{
[Activity(Label = "VoicRec2", MainLauncher = true)]
public class MainActivity : Activity
{
MediaRecorder _recorder;
MediaPlayer _player;
Button _start;
Button _stop;
string path;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_start = FindViewById<Button>(Resource.Id.start);
_stop = FindViewById<Button>(Resource.Id.stop);
path = Path.Combine(this.GetExternalFilesDir(Android.OS.Environment.DirectoryMusic).AbsolutePath, "MyWav.wav");
_start.Click += delegate {
_stop.Enabled = !_stop.Enabled;
_start.Enabled = !_start.Enabled;
_recorder.SetAudioSource(AudioSource.Mic);
_recorder.SetOutputFormat(OutputFormat.ThreeGpp);
_recorder.SetAudioChannels(1);
_recorder.SetAudioSamplingRate(16);
_recorder.SetAudioEncodingBitRate(16000);
_recorder.SetAudioEncoder((AudioEncoder) Encoding.Pcm16bit);
_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;
};
}
public void onStart()
{
}
protected override void OnPause()
{
base.OnPause();
_player.Release();
_recorder.Release();
_player.Dispose();
_recorder.Dispose();
_player = null;
_recorder = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.