Skip to content

Instantly share code, notes, and snippets.

@Xonshiz Xonshiz/MainActivity.cs
Last active Mar 15, 2018

Embed
What would you like to do?
using Android.App;
using Android.Widget;
using Android.OS;
using System.IO;
using Android.Media;
using System.Net.Http;
using System.Net.Http.Headers;
using System;
using System.Text;
using System.Web;
using System.Threading;
namespace VoicRec2
{
[Activity(Label = "VoicRec2", MainLauncher = true)]
public class MainActivity : Activity
{
AudioRecord audRecorder;
AudioTrack audioTrack;
volatile bool _isRecording = false;
private byte[] audioBuffer;
private int audioData;
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) Android.Media.Encoding.Pcm16bit);
//_recorder.SetOutputFile(path);
//_recorder.Prepare();
//_recorder.Start();
_isRecording = true;
new Thread(delegate ()
{
RecordAudio();
}).Start();
_start.Enabled = false;
_stop.Enabled = true;
};
_stop.Click += delegate {
//_stop.Enabled = !_stop.Enabled;
//_recorder.Stop();
//_recorder.Reset();
//_player.SetDataSource(path);
//_player.Prepare();
//_player.Start();
//Toast.MakeText(this, "Sending", ToastLength.Long);
//MakeRequest();
//Toast.MakeText(this, "Sent", ToastLength.Long);
if (_isRecording == true)
{
_isRecording = false;
audRecorder.Stop();
audRecorder.Release();
_start.Enabled = true;
_stop.Enabled = true;
}
byte[] fileData = File.ReadAllBytes(path);
new Thread(delegate ()
{
PlayAudioTrack(fileData);
}).Start();
_start.Enabled = true;
_stop.Enabled = true;
if (File.Exists(path))
{
byte[] filebytes = File.ReadAllBytes(path);
Console.WriteLine("#########################################################################################################################");
}
};
}
public void RecordAudio()
{
System.IO.Stream outputStream = System.IO.File.Open(path, FileMode.Create);
BinaryWriter bWriter = new BinaryWriter(outputStream);
audioBuffer = new byte[8000];
audRecorder = new AudioRecord(
// Hardware source of recording.
AudioSource.Mic,
// Frequency
16000,
// Mono or stereo
ChannelIn.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length
);
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = 16000;
int channels = 1;
long byteRate = 16 * longSampleRate * channels / 8;
totalAudioLen = audioBuffer.Length;
totalDataLen = totalAudioLen + 36;
WriteWaveFileHeader(
bWriter,
totalAudioLen,
totalDataLen,
longSampleRate,
channels,
byteRate);
audRecorder.StartRecording();
while (_isRecording == true)
{
try
{
/// Keep reading the buffer while there is audio input.
audioData = audRecorder.Read(audioBuffer, 0, audioBuffer.Length);
bWriter.Write(audioBuffer);
}
catch (System.Exception ex)
{
Console.Out.WriteLine(ex.Message);
break;
}
}
outputStream.Close();
bWriter.Close();
}
private void WriteWaveFileHeader(BinaryWriter bWriter, long totalAudioLen,
long totalDataLen, long longSampleRate, int channels,
long byteRate)
{
byte[] header = new byte[44];
header[0] = (byte)'R'; // RIFF/WAVE header
header[1] = (byte)'I';
header[2] = (byte)'F';
header[3] = (byte)'F';
header[4] = (byte)(totalDataLen & 0xff);
header[5] = (byte)((totalDataLen >> 8) & 0xff);
header[6] = (byte)((totalDataLen >> 16) & 0xff);
header[7] = (byte)((totalDataLen >> 24) & 0xff);
header[8] = (byte)'W';
header[9] = (byte)'A';
header[10] = (byte)'V';
header[11] = (byte)'E';
header[12] = (byte)'f'; // 'fmt ' chunk
header[13] = (byte)'m';
header[14] = (byte)'t';
header[15] = (byte)' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (byte)channels;
header[23] = 0;
header[24] = (byte)(longSampleRate & 0xff);
header[25] = (byte)((longSampleRate >> 8) & 0xff);
header[26] = (byte)((longSampleRate >> 16) & 0xff);
header[27] = (byte)((longSampleRate >> 24) & 0xff);
header[28] = (byte)(byteRate & 0xff);
header[29] = (byte)((byteRate >> 8) & 0xff);
header[30] = (byte)((byteRate >> 16) & 0xff);
header[31] = (byte)((byteRate >> 24) & 0xff);
header[32] = (byte)(2 * 16 / 8); // block align
header[33] = 0;
header[34] = 16; // bits per sample
header[35] = 0;
header[36] = (byte)'d';
header[37] = (byte)'a';
header[38] = (byte)'t';
header[39] = (byte)'a';
header[40] = (byte)(totalAudioLen & 0xff);
header[41] = (byte)((totalAudioLen >> 8) & 0xff);
header[42] = (byte)((totalAudioLen >> 16) & 0xff);
header[43] = (byte)((totalAudioLen >> 24) & 0xff);
bWriter.Write(header, 0, 44);
}
void PlayAudioTrack(byte[] audBuffer)
{
audioTrack = new AudioTrack(
// Stream type
Android.Media.Stream.Music,
// Frequency
16000,
// Mono or stereo
ChannelOut.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audBuffer.Length,
// Mode. Stream or static.
AudioTrackMode.Stream);
audioTrack.Play();
audioTrack.Write(audBuffer, 0, audBuffer.Length);
}
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.