Hi, i'm using the "sdk 1.5", built from source code, and i enable
(removed @hide directive) audio streaming and all media files. I
created the eclipse plugin as well.
I'm testing the AudioRecord class, and in a simple activity,
when i instanciate the class, an error appear in log cat:
02-02 10:38:33.939: ERROR/AudioRecord-JNI(200): Error creating
AudioRecord instance: initialization check failed.
02-02 10:38:33.997: ERROR/AudioRecord-Java(200):
[ android.media.AudioRecord ] Error code -8 when initializing native
AudioRecord object.
I looked in source code, and, this error could be caused by permission
in audio hardware. But i 'm not been able to solve this issue. Does
anybody have idea of what is happenning? below my code as well my
manifest.
package com.ttt;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.media.AmrInputStream;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class AudioStreaming extends Activity {
public static final int AUDIO_SAMPLE_FREQUENCY_11025HZ = 11025;
public static final int AMR_BUFFER_SIZE = 140;
public static final int AUDIO_BUFFER_SIZE = 5 * AMR_BUFFER_SIZE;
public static final int AUDIO_TIMER = 200;
public static final int AUDIO_TIMER_DELAY = 5;
public static final String path = "/sdcard/stream.amr";
private Timer mAudioTimer;
public ToggleButton m_bt_button;
public TextView m_tv_text;
public static boolean isRecording = false;
public AudioRecord audioRecord = null;
public MediaRecorder recorder;
public AmrInputStream amrInputStream;
public byte[] audioBuffer = new byte[AUDIO_BUFFER_SIZE];
public byte[] amrAudioBuffer;
public int offset = 0;
public int bytesRead = 0;
public int bytesConverted = 0;
public InputStream is;
public ByteArrayInputStream bais;
public VoicePlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_bt_button = (ToggleButton) findViewById(R.id.bt_record);
m_tv_text = (TextView) findViewById(R.id.tv_info);
m_tv_text.setText("Stopped");
m_bt_button.setOnClickListener(mRecord);
try {
// ------- HERE THE CODE PROGRAM CRASHES
WITHOUT LAUNCH EXCEPTION
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
AUDIO_SAMPLE_FREQUENCY_11025HZ,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, AUDIO_BUFFER_SIZE);
} catch (IllegalArgumentException e){
Log.e("Audio Stream", "Exception while creating audioRecord: " +
e.getMessage());
}
player = new VoicePlayer(path);
}
public void startAudioTimer() {
if (mAudioTimer == null) {
audioRecord.startRecording();
mAudioTimer = new Timer();
mAudioTimer.scheduleAtFixedRate(new AudioTask(), AUDIO_TIMER_DELAY,
AUDIO_TIMER);
Log.i("Audio Streaming Teste", "Audio Timer Started");
}
}
public void stopAudioTimer() {
if (mAudioTimer != null) {
audioRecord.stop();
mAudioTimer = null;
Log.i("Audio Streaming Teste", "Audio Timer Stopped");
}
}
private class AudioTask extends TimerTask {
public AudioTask() {
super();
}
@Override
public void run() {
bytesRead = audioRecord.read(audioBuffer, offset, AMR_BUFFER_SIZE);
is = new ByteArrayInputStream(audioBuffer, offset,
AMR_BUFFER_SIZE);
amrInputStream = new AmrInputStream(is);
try {
bytesConverted = amrInputStream.read(amrAudioBuffer, offset,
AMR_BUFFER_SIZE);
if (bytesConverted == -1) {
stopAudioTimer();
player.play();
}
else {
if (bytesConverted == bytesRead) {
player.writeAmrFile(amrAudioBuffer);
}
else {
Log.e("Audio Player", "bytes converted its different from bytes
read");
}
}
} catch (IOException e) {
Log.e("Audio Streaming", "Exception while converting from PCM16 to
AMR: " + e.getMessage());
}
}
}
private OnClickListener mRecord = new OnClickListener() {
public void onClick(View arg0) {
isRecording ^= true;
if (isRecording) {
startAudioTimer();
m_tv_text.setText("Recording...");
} else {
stopAudioTimer();
try {
player.play();
m_tv_text.setText("playing...");
} catch (FileNotFoundException e) {
Log.e("Audio Player", "Exception: " + e.getMessage());
} catch (IOException e) {
Log.e("Audio Player", "Exception: " + e.getMessage());
}
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.mot"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECORD_AUDIO"/
>
<application android:icon="@drawable/icon" android:label="@string/
app_name">
<activity android:name=".AudioStreaming"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Thanks a lot
Breno