每当我尝试在模拟器中运行此应用程序时,我都会收到此错误:
09-11 05:34:08.580 859-859/com.spookyapps.lollatone D/dalvikvm﹕ GC_FOR_ALLOC freed 54K, 10% free 3448K/3792K, paused 28ms, total 29ms
09-11 05:34:08.650 859-859/com.spookyapps.lollatone I/dalvikvm-heap﹕ Grow heap (frag case) to 15.450MB for 12381028-byte allocation
09-11 05:34:08.680 859-868/com.spookyapps.lollatone D/dalvikvm﹕ GC_FOR_ALLOC freed 2K, 3% free 15536K/15884K, paused 28ms, total 28ms
09-11 05:34:09.410 859-859/com.spookyapps.lollatone D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 3% free 15760K/16108K, paused 27ms, total 27ms
09-11 05:34:09.430 859-859/com.spookyapps.lollatone I/dalvikvm-heap﹕ Grow heap (frag case) to 18.084MB for 2536936-byte allocation
09-11 05:34:09.470 859-868/com.spookyapps.lollatone D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 2% free 18237K/18588K, paused 32ms, total 32ms
09-11 05:34:09.640 859-859/com.spookyapps.lollatone D/AndroidRuntime﹕ Shutting down VM
09-11 05:34:09.640 859-859/com.spookyapps.lollatone W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a19ba8)
09-11 05:34:09.650 859-872/com.spookyapps.lollatone W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1a19ba8)
09-11 05:34:09.650 859-872/com.spookyapps.lollatone I/Process﹕ Sending signal. PID: 859 SIG: 9
这是我的主要活动
package com.spookyapps.lollatone;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Sounds mSound = new Sounds();
Thread thread = new Thread(new Runnable() {
public void run() {
mSound.record();
}
});
thread.start();
try {
wait(10000);
} catch (InterruptedException e) {
Log.e("Record", "wait failed");
}
//Sounds.mIsRecording = false;
try {
thread.join();
} catch (InterruptedException e) {
Log.e("Thread", "Join failed");
}
mSound.play();
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
声音等级
package com.spookyapps.lollatone;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by abc on 9/7/2014.
*/
public class Sounds {
public boolean mIsRecording = false;
public void record() {
int frequency = 11025;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
//change later to create new file like "_1" "_2" etc based on constant
if (file.exists()) {
file.delete();
}
//create the new file
try {
file.createNewFile();
} catch (IOException e) {
throw new IllegalStateException("Failed to create" + file.toString());
}
try {
// Create a DataOutputStream to write the audio data into the saved file
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
// Create a new AudioRecord object to record the audio
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
frequency, channelConfiguration,
audioEncoding, bufferSize);
short[] buffer = new short[bufferSize];
audioRecord.startRecording();
mIsRecording = true;
while (mIsRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
for (int i = 0; i < bufferReadResult; i++) {
dos.writeShort(buffer[i]);
}
}
audioRecord.stop();
mIsRecording = false;
dos.close();
} catch (Throwable t) {
Log.e("AudioRecord", "Recording Failed");
}
}
public void play() {
// get the file to play back
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/reverseme.pcm");
//Get the length of the audio stored in the file (16 bit so 2 bytes per short)
//and create a short array to store the recorded audio
int musicLength = (int) (file.length());
short[] music = new short[musicLength];
try {
//Create a DataInputStream to read the audio data back from the saved file
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
//Read the file into the music array
int i = 0;
while (dis.available() > 0) {
music[musicLength - 1 - i] = dis.readShort();
i++;
}
// close the input streams
dis.close();
// Create a new AudioTrack object using the same parameters as the AudioRecord
// object used to create the file
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
musicLength,
AudioTrack.MODE_STREAM);
// Start playback
audioTrack.play();
//Write the music buffer to the AudioTrack object
audioTrack.write(music, 0, musicLength);
} catch (Throwable t) {
Log.e("AudioTrack", "Playback Failed");
}
}
}
它应该会自动尝试录制音频,然后在应用启动后播放它但应用程序永远不会启动,我只是在logcat中获取这些消息
编辑:新错误:
09-11 05:53:07.631 1143-1143 / com.spookyapps.lollatone E / AndroidRuntime:FATAL EXCEPTION:main
Process: com.spookyapps.lollatone, PID: 1143
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spookyapps.lollatone/com.spookyapps.lollatone.MainActivity}: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:401)
at com.spookyapps.lollatone.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先退出eclipse
转到目录“C:\ Users \ Admin.android”并删除avd,缓存文件夹和&amp; adbkey.pub。
然后重新启动eclipse make new avd然后运行你的活动/应用程序。
它绝对可以运行。