Apps Low Level AudioRecorder issue. (Using AudioRecorder)

FrankIQ

Lurker
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.
 

Unforgiven

...eschew obfuscation...
Administrator
Application Development thread moved to the Development forum for better exposure.:)

Also, if you could put the code in code tags it makes it easier for other coders to read and lend a hand.:)

[code]
line 1
line 2
...
...
line x
[/code]


Code:
line 1
line 2
...
...
line x
 
D

Deleted User

Guest
Please state what the error is. You should include the stack trace from your Logcat output.
Your problem is almost impossible to diagnose without this information.
Thanks.
 
D

Deleted User

Guest
The relevant part of the Logcat is the fatal exception part, and associated stack trace.
Any time an app crashes unexpectedly it's due to receiving an exception of some kind. You can see here that the app is trying to access a file which it's not allowed to open.

Code:
01-07 08:52:24.070 E/AndroidRuntime( 1002): FATAL EXCEPTION: main

01-07 08:52:24.070 E/AndroidRuntime( 1002): Process: AudioRecordTEst.AudioRecordTEst, PID: 1002

01-07 08:52:24.070 E/AndroidRuntime( 1002): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

01-07 08:52:24.070 E/AndroidRuntime( 1002):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

01-07 08:52:24.070 E/AndroidRuntime( 1002):     at dalvik.system.NativeStart.main(Native Method)

01-07 08:52:24.070 E/AndroidRuntime( 1002): Caused by: java.lang.reflect.InvocationTargetException

01-07 08:52:24.070 E/AndroidRuntime( 1002):     at java.lang.reflect.Method.invokeNative(Native Method)

01-07 08:52:24.070 E/AndroidRuntime( 1002):     at java.lang.reflect.Method.invoke(Method.java:515)

01-07 08:52:24.070 E/AndroidRuntime( 1002):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)

01-07 08:52:24.070 E/AndroidRuntime( 1002):     ... 2 more

01-07 08:52:24.070 E/AndroidRuntime( 1002): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.UnauthorizedAccessException: Access to the path "/data/data/Example_WorkingWithAudio.Example_WorkingWithAudio/files/testAudio.mp4" is denied.
 

FrankIQ

Lurker
Thread starter
What it could be, i have read that the app need permissions for these tasks, but i already added them:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

edit: Trying with others permissions. Lets see
 
D

Deleted User

Guest
Probably your full file path does not exist. Make sure that all the intermediate directories are created before trying to create the mp4 file
 
Top