我正在开发一个android应用程序,任何地方都没有错误,但是当我在手机上运行它时,在应用程序打开并单击一个按钮后,过了一会儿它突然关闭并给我消息“不幸的是** 已经停止”。
有人可以告诉我有什么问题吗?
我做了一些研究,发现当清单中有问题时会出现此消息。这是我的清单,我不知道出了什么问题?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tryagain"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>
这是我的 .java :
public class MainActivity extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
private Thread recordingThread = null;
private boolean isRecording = false;
ArrayList <Short> x=new ArrayList<Short>();
ArrayList <Short> y=new ArrayList<Short>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButtonHandlers();
enableButtons(false);
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
//why is the following statement here???
System.out.println("BUFFER SIZE VALUE IS " + bufferSize);
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
((Button) findViewById(R.id.Start2)).setOnClickListener(btnClick2);
((Button) findViewById(R.id.Stop2)).setOnClickListener(btnClick2);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnStop, isRecording);
enableButton(R.id.Start2, !isRecording);
enableButton(R.id.Stop2, isRecording);
}
int BufferElements2Rec = 1024;
int BytesPerElement = 2; // 2 bytes in 16bit format
private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private void startRecording2() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile2();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private byte[] short2byte(short[] sData) {
int shortArrsize = sData.length;
byte[] bytes = new byte[shortArrsize * 2];
for (int i = 0; i < shortArrsize; i++) {
bytes[i * 2] = (byte) (sData[i] & 0x00FF);
bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
sData[i] = 0;
}
return bytes;
}
private void writeAudioDataToFile() {
// Write the output audio in byte
String filePath = "/storage/sdcard0/Sounds/recording1.pcm";
short sData[] = new short[BufferElements2Rec];
// to convert sData from short to Short:
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format and stores it in buffer sData
recorder.read(sData, 0, BufferElements2Rec);
// here u will write the new code: to store what was said in an array,,, here we are
Short sData1= sData[BufferElements2Rec];
int i=0;
x.add(i,sData1);
i++;
System.out.println("Short wirting to file" + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} //END OF WRITE TO FILE METHOD
private void writeAudioDataToFile2() {
// Write the output audio in byte
String filePath = "/storage/sdcard0/Sounds/recording2.pcm";
short sData[] = new short[BufferElements2Rec];
// to convert sData from short to Short:
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format and stores it in buffer sData
recorder.read(sData, 0, BufferElements2Rec);
// here u will write the new code: to store what was said in an array,,, here we are
Short sData1= sData[BufferElements2Rec];
int i=0;
y.add(i,sData1);
i++;
System.out.println("Short wirting to file" + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} //END OF WRITE TO FILE METHOD
private void stopRecording() {
// stops the recording activity
if (null != recorder) {
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
}
private View.OnClickListener btnClick = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
enableButtons(false);
stopRecording();
break;
}
}
}
};
private View.OnClickListener btnClick2 = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.Start2: {
enableButtons(true);
startRecording2();
break;
}
case R.id.Stop2: {
enableButtons(false);
stopRecording();
break;
}
}
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
这是我的 logCat
06-27 16:50:46.830: I/System.out(11939): BUFFER SIZE VALUE IS 2048
06-27 16:50:46.980: D/libEGL(11939): loaded /vendor/lib/egl/libGLES_vc4.so
06-27 16:50:46.980: D/(11939): SpyHook inactive - could not find libspyhook.so [Symbol not found: ] or not enabled (debug.egl.hw.spy = 0)
06-27 16:50:47.000: W/khrn_client(11939): init_window num_buffers 3 min undequeued buffers 1 type 1
06-27 16:50:47.000: W/khrn_client(11939): init_window window 0x50eda0d8, 480x800 hintTransform 0x0
06-27 16:50:47.030: D/OpenGLRenderer(11939): Enabling debug mode 0
06-27 16:50:53.066: W/dalvikvm(11939): threadid=12: thread exiting with uncaught exception (group=0x40fb72a0)
06-27 16:50:53.066: E/AndroidRuntime(11939): FATAL EXCEPTION: AudioRecorder Thread
06-27 16:50:53.066: E/AndroidRuntime(11939): java.lang.ArrayIndexOutOfBoundsException: length=1024; index=1024
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.access$4(MainActivity.java:125)
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity$3.run(MainActivity.java:84)
06-27 16:50:53.066: E/AndroidRuntime(11939): at java.lang.Thread.run(Thread.java:856)