Rad*_*hiu 5 java audio android record amplitude
我正在尝试创建一个记录声音一秒钟的应用程序,然后从录制的声音中读取最大振幅.这是我到目前为止,但我的应用程序崩溃,我无法弄清楚为什么.
这是我的第二个活动,在我按下"录制"按钮后,我从第一个活动开始.该应用程序在我的模拟器和手机上崩溃.
package radu.soundSampler;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.media.MediaRecorder;
public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Intent intent = getIntent();
//String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
MediaRecorder rec = new MediaRecorder();
rec.setAudioSource(MediaRecorder.AudioSource.MIC);
rec.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
rec.setOutputFile("/newRecording");
rec.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
rec.prepare();
} catch (IllegalStateException e) {
} catch (IOException o) {
}
rec.start();
for(int i=0;i<100000;i++);
rec.stop();
int rezultat = rec.getMaxAmplitude();
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(Integer.toString(rezultat));
setContentView(textView);
}
}
Run Code Online (Sandbox Code Playgroud)
我计算机上的(更新的)错误日志说:
08-18 13:30:07.968: E/MediaRecorder(687): start called in an invalid state: 4
08-18 13:30:07.968: D/AndroidRuntime(687): Shutting down VM
08-18 13:30:07.968: W/dalvikvm(687): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-18 13:30:07.978: E/AndroidRuntime(687): FATAL EXCEPTION: main
08-18 13:30:07.978: E/AndroidRuntime(687): java.lang.RuntimeException: Unable to start activity ComponentInfo{radu.soundSampler/radu.soundSampler.DisplayMessageActivity}: java.lang.IllegalStateException
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.os.Looper.loop(Looper.java:123)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 13:30:07.978: E/AndroidRuntime(687): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 13:30:07.978: E/AndroidRuntime(687): at java.lang.reflect.Method.invoke(Method.java:507)
08-18 13:30:07.978: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 13:30:07.978: E/AndroidRuntime(687): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 13:30:07.978: E/AndroidRuntime(687): at dalvik.system.NativeStart.main(Native Method)
08-18 13:30:07.978: E/AndroidRuntime(687): Caused by: java.lang.IllegalStateException
08-18 13:30:07.978: E/AndroidRuntime(687): at android.media.MediaRecorder.start(Native Method)
08-18 13:30:07.978: E/AndroidRuntime(687): at radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:29)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 13:30:07.978: E/AndroidRuntime(687): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 13:30:07.978: E/AndroidRuntime(687): ... 11 more
Run Code Online (Sandbox Code Playgroud)
所以我想也许模拟器不能"运行"麦克风,所以我安装.apk在我的手机上,但它也崩溃了.我不知道会出现什么问题,所以请你帮我,和/或给我任何提示?
非常感谢提前!
一个堆栈跟踪,您请求的堆栈跟踪.我在catch语句中使用o.printStackTrace()和e.printStackTrace(),我希望这是你要求我做的:
08-18 14:06:11.908: W/System.err(811): java.io.FileNotFoundException: /newRecording (Read-only file system)
08-18 14:06:11.908: W/System.err(811): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
08-18 14:06:11.908: W/System.err(811): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
08-18 14:06:11.918: W/System.err(811): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
08-18 14:06:11.918: W/System.err(811): at java.io.FileOutputStream.<init>(FileOutputStream.java:165)
08-18 14:06:11.918: W/System.err(811): at java.io.FileOutputStream.<init>(FileOutputStream.java:144)
08-18 14:06:11.918: W/System.err(811): at android.media.MediaRecorder.prepare(MediaRecorder.java:533)
08-18 14:06:11.918: W/System.err(811): at radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
08-18 14:06:11.918: W/System.err(811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 14:06:11.918: W/System.err(811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 14:06:11.918: W/System.err(811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 14:06:11.918: W/System.err(811): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-18 14:06:11.918: W/System.err(811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-18 14:06:11.918: W/System.err(811): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:06:11.918: W/System.err(811): at android.os.Looper.loop(Looper.java:123)
08-18 14:06:11.918: W/System.err(811): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 14:06:11.918: W/System.err(811): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:06:11.928: W/System.err(811): at java.lang.reflect.Method.invoke(Method.java:507)
08-18 14:06:11.928: W/System.err(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 14:06:11.928: W/System.err(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 14:06:11.928: W/System.err(811): at dalvik.system.NativeStart.main(Native Method)
08-18 14:06:11.928: E/MediaRecorder(811): start called in an invalid state: 4
08-18 14:06:11.928: D/AndroidRuntime(811): Shutting down VM
08-18 14:06:11.928: W/dalvikvm(811): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-18 14:06:11.948: E/AndroidRuntime(811): FATAL EXCEPTION: main
08-18 14:06:11.948: E/AndroidRuntime(811): java.lang.RuntimeException: Unable to start activity ComponentInfo{radu.soundSampler/radu.soundSampler.DisplayMessageActivity}: java.lang.IllegalStateException
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.os.Looper.loop(Looper.java:123)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 14:06:11.948: E/AndroidRuntime(811): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:06:11.948: E/AndroidRuntime(811): at java.lang.reflect.Method.invoke(Method.java:507)
08-18 14:06:11.948: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 14:06:11.948: E/AndroidRuntime(811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 14:06:11.948: E/AndroidRuntime(811): at dalvik.system.NativeStart.main(Native Method)
08-18 14:06:11.948: E/AndroidRuntime(811): Caused by: java.lang.IllegalStateException
08-18 14:06:11.948: E/AndroidRuntime(811): at android.media.MediaRecorder.start(Native Method)
08-18 14:06:11.948: E/AndroidRuntime(811): at radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:30)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 14:06:11.948: E/AndroidRuntime(811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 14:06:11.948: E/AndroidRuntime(811): ... 11 more
08-18 14:06:14.181: I/Process(811): Sending signal. PID: 811 SIG: 9
Run Code Online (Sandbox Code Playgroud)
当您尝试使用 时,无法在根文件系统中创建文件rec.setOutputFile("/newRecording")。创建一个临时文件并将其写入其中。您可能想检查获取私有文件目录getFilesDir()及相关方法。
您得到的异常准确地告诉您这一点:FileNotFoundException发生并显示调用prepare()失败。因此,您还MediaRecoder没有准备好,并且您将无法准备start(),因为它处于错误的状态。
另一条评论:要在停止录制之前等待一秒钟,请使用Thread.sleep()而不是for循环计数。其次,请在后台线程中执行此操作,因为以任何方式阻塞 UI 线程都可能会使您的 UI 变得迟缓,并且在最坏的情况下,Android 可能会因 ANR 异常而终止您的应用程序。
| 归档时间: |
|
| 查看次数: |
4960 次 |
| 最近记录: |