I could run the example on my emulator without problems. https://github.com/xamarin/monodroi...ple_WorkingWithAudio/Example_WorkingWithAudio
But i want to make my own project using this code belows and i can't make it works . When i press the Record button the app stops and gives error.
CODE:
`
public class MainActivity : Activity
{
bool isrecording =false;
AudioRecord audRecorder =null;
byte[] audioBuffer =null;
staticstring filePath ="/data/data/Example_WorkingWithAudio.Example_WorkingWithAudio/files/testAudio.mp4";
protectedoverridevoidOnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button =FindViewById<Button>(Resource.Id.MyButton);//RECORD BUTTON
button.Click+=delegate{
isrecording =true;
this.RecordAudio();
};
Button button2 =FindViewById<Button>(Resource.Id.MyButton2);//STOP BUTTON
button2.Click+=delegate{
isrecording =false;
audRecorder.Stop();
audRecorder.Release();
};
Button button3 =FindViewById<Button>(Resource.Id.MyButton3);//PLAYBACK BUTTON
button3.Click+=delegate{
byte[] fileData =File.ReadAllBytes(filePath);
this.PlayAudioTrack(fileData);
};
}
privatevoidRecordAudio()
{
var fileStream = new FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
audioBuffer =newbyte[8000];
audRecorder =newAudioRecord(
// Hardware source of recording.
AudioSource.Mic,
// Frequency
11025,
// Mono or stereo
ChannelIn.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length
);
audRecorder.StartRecording();
while(isrecording){
try
{
// Keep reading the buffer while there is audio input.
int numBytes = audRecorder.Read(audioBuffer,0, audioBuffer.Length);
fileStream.Write(audioBuffer,0, numBytes);
// Write out the audio file.
}catch(Exception ex){
Console.Out.WriteLine(ex.Message);
break;
}
}
fileStream.Close();
}
voidPlayAudioTrack(byte[] audioBuffer)
{
AudioTrack audioTrack =newAudioTrack(
// Stream type
Android.Media.Stream.Music,
// Frequency
11025,
// Mono or stereo
ChannelOut.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length,
// Mode. Stream or static.
AudioTrackMode.Stream);
audioTrack.Play();
audioTrack.Write(audioBuffer,0, audioBuffer.Length);
}
}<del></del>
`
Help would be appreciated.
But i want to make my own project using this code belows and i can't make it works . When i press the Record button the app stops and gives error.
CODE:
`
public class MainActivity : Activity
{
bool isrecording =false;
AudioRecord audRecorder =null;
byte[] audioBuffer =null;
staticstring filePath ="/data/data/Example_WorkingWithAudio.Example_WorkingWithAudio/files/testAudio.mp4";
protectedoverridevoidOnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button =FindViewById<Button>(Resource.Id.MyButton);//RECORD BUTTON
button.Click+=delegate{
isrecording =true;
this.RecordAudio();
};
Button button2 =FindViewById<Button>(Resource.Id.MyButton2);//STOP BUTTON
button2.Click+=delegate{
isrecording =false;
audRecorder.Stop();
audRecorder.Release();
};
Button button3 =FindViewById<Button>(Resource.Id.MyButton3);//PLAYBACK BUTTON
button3.Click+=delegate{
byte[] fileData =File.ReadAllBytes(filePath);
this.PlayAudioTrack(fileData);
};
}
privatevoidRecordAudio()
{
var fileStream = new FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
audioBuffer =newbyte[8000];
audRecorder =newAudioRecord(
// Hardware source of recording.
AudioSource.Mic,
// Frequency
11025,
// Mono or stereo
ChannelIn.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length
);
audRecorder.StartRecording();
while(isrecording){
try
{
// Keep reading the buffer while there is audio input.
int numBytes = audRecorder.Read(audioBuffer,0, audioBuffer.Length);
fileStream.Write(audioBuffer,0, numBytes);
// Write out the audio file.
}catch(Exception ex){
Console.Out.WriteLine(ex.Message);
break;
}
}
fileStream.Close();
}
voidPlayAudioTrack(byte[] audioBuffer)
{
AudioTrack audioTrack =newAudioTrack(
// Stream type
Android.Media.Stream.Music,
// Frequency
11025,
// Mono or stereo
ChannelOut.Mono,
// Audio encoding
Android.Media.Encoding.Pcm16bit,
// Length of the audio clip.
audioBuffer.Length,
// Mode. Stream or static.
AudioTrackMode.Stream);
audioTrack.Play();
audioTrack.Write(audioBuffer,0, audioBuffer.Length);
}
}<del></del>
`
Help would be appreciated.