运行时出错,录制音频
Posted
技术标签:
【中文标题】运行时出错,录制音频【英文标题】:Error when running, recording audio 【发布时间】:2014-09-11 09:40:25 【问题描述】:每当我尝试在模拟器中运行此应用程序时,我都会收到此错误:
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 ComponentInfocom.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)
【问题讨论】:
logcat中肯定有更多,这个GC_ALLOC消息与关闭无关。这真的是你得到的全部吗? 我认为你也应该发布你的声音课程的重要部分 抱歉,第三条评论 :)...为什么您要完成活动?如果您直接完成活动,则没有机会播放声音,请删除finish();如果您想在声音结束时完成活动,您必须以另一种方式完成.. 我在上面添加了声音类。我还删除了finish(),现在在上面编辑了这些错误 我认为导致此问题的原因与您的线程/等待/通知有关。但这是我直到现在还没有使用过的东西,而且我没有 IDE 来测试一些东西,所以我不能给你正确的答案。但也许这个链接会对你有所帮助:***.com/questions/17811794/… 【参考方案1】:第一次退出eclipse
转到目录“C:\Users\Admin.android”并删除 avd、缓存文件夹和 adbkey.pub。
然后重新启动 eclipse make new avd 然后运行您的活动/应用程序。
它肯定会运行的。
【讨论】:
使用androidStudio。但我照你说的做了,仍然得到这个:09-11 15:46:50.230 1049-1049/com.spookyapps.lollatone D/AndroidRuntime: 关闭 VM 09-11 15:46:50.230 1049-1049/com.spookyapps .lollatone W/dalvikvm: threadid=1: 线程退出未捕获异常 (group=0xb1a87ba8) 09-11 15:46:50.240 1049-1062/com.spookyapps.lollatone W/dalvikvm: threadid=11: 线程退出未捕获异常(组=0xb1a87ba8)以上是关于运行时出错,录制音频的主要内容,如果未能解决你的问题,请参考以下文章
使用 Python 录制或播放音频无法在 Mac 上运行:没有错误和没有声音