在Android中录制声音,并宣读幅度 [英] Record sound in Android and read Amplitude

查看:168
本文介绍了在Android中录制声音,并宣读幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,它记录的声音一秒钟,然后读取录制的声音最大振幅。这是我有这么远,但我的应用程序崩溃,我想不出为什么。

这是我的第二个活动,这我是从我的第一个呼叫后,我preSS一个记录按钮。该应用程序崩溃在我的模拟器,并在我的手机。

 包radu.soundSampler;

进口java.io.IOException异常;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.TextView;
进口android.media.MediaRecorder;

公共类DisplayMessageActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //意向意图= getIntent();
    //字符串消息= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    MediaRecorder REC =新MediaRecorder();
    rec.setAudioSource(MediaRecorder.AudioSource.MIC);
    rec.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
    rec.setOutputFile(/ newRecording);
    rec.setAudioEn codeR(MediaRecorder.AudioEn coder.DEFAULT);

    尝试 {
        REC prepare()。
    }赶上(IllegalStateException异常E){

    }赶上(IOException异常O){

    }
    rec.start();
            的for(int i = 0; I< 100000;我++);
    rec.stop();
    INT rezultat = rec.getMaxAmplitude();

    TextView中的TextView =新的TextView(本);
    textView.setTextSize(40);

    textView.setText(Integer.toString(rezultat));

    的setContentView(TextView的);
}

}
 

的(更新)我的计算机上的错误日志说:

  08-18 13:30:​​07.968:E / MediaRecorder(687):启动名为处于无效状态:4
08-18 13:30:​​07.968:D / AndroidRuntime(687):关闭虚拟机
08-18 13:30:​​07.968:W / dalvikvm(687):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
08-18 13:30:​​07.978:E / AndroidRuntime(687):致命异常:主要
08-18 13:30:​​07.978:E / AndroidRuntime(687):java.lang.RuntimeException的:无法启动的活动ComponentInfo {radu.soundSampler / radu.soundSampler.DisplayMessageActivity}:java.lang.IllegalStateException
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.os.Handler.dispatchMessage(Handler.java:99)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.os.Looper.loop(Looper.java:123)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在java.lang.reflect.Method.invokeNative(本机方法)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在java.lang.reflect.Method.invoke(Method.java:507)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在dalvik.system.NativeStart.main(本机方法)
08-18 13:30:​​07.978:E / AndroidRuntime(687):致:java.lang.IllegalStateException
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.media.MediaRecorder.start(本机方法)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:29)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 13:30:​​07.978:E / AndroidRuntime(687):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 13:30:​​07.978:E / AndroidRuntime(687):11 ...更多
 

所以我想也许模拟器不能跑的麦克风,所以我安装了 .apk文件在我的手机,但它坠毁那里了。我不知道,可能是什么问题,所以请你帮助我,和/或给我任何提示?

感谢很多提前!

还有一个堆栈跟踪,你要求的人。我用在catch语句 o.printStackTrace() e.printStackTrace(),我希望这是你的要求是什么我做的:

  08-18 14:06:11.908:W / System.err的(811):java.io.FileNotFoundException:/ newRecording(只读文件系统)
08-18 14:06:11.908:W / System.err的(811):在org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)
08-18 14:06:11.908:W / System.err的(811):在dalvik.system.BlockGuard $ WrappedFileSystem.open(BlockGuard.java:232)
08-18 14:06:11.918:W / System.err的(811):在java.io.FileOutputStream中的< INIT>(FileOutputStream.java:94)
08-18 14:06:11.918:W / System.err的(811):在java.io.FileOutputStream中的< INIT>(FileOutputStream.java:165)
08-18 14:06:11.918:W / System.err的(811):在java.io.FileOutputStream中的< INIT>(FileOutputStream.java:144)
08-18 14:06:11.918:W / System.err的(811):在android.media.MediaRecorder prepare(MediaRecorder.java:533)
08-18 14:06:11.918:W / System.err的(811):在radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:23)
08-18 14:06:11.918:W / System.err的(811):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 14:06:11.918:W / System.err的(811):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 14:06:11.918:W / System.err的(811):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 14:06:11.918:W / System.err的(811):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-18 14:06:11.918:W / System.err的(811):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
08-18 14:06:11.918:W / System.err的(811):在android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:06:11.918:W / System.err的(811):在android.os.Looper.loop(Looper.java:123)
08-18 14:06:11.918:W / System.err的(811):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 14:06:11.918:W / System.err的(811):在java.lang.reflect.Method.invokeNative(本机方法)
08-18 14:06:11.928:W / System.err的(811):在java.lang.reflect.Method.invoke(Method.java:507)
08-18 14:06:11.928:W / System.err的(811):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 14:06:11.928:W / System.err的(811):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 14:06:11.928:W / System.err的(811):在dalvik.system.NativeStart.main(本机方法)
08-18 14:06:11.928:E / MediaRecorder(811):启动名为处于无效状态:4
08-18 14:06:11.928:D / AndroidRuntime(811):关闭虚拟机
08-18 14:06:11.928:W / dalvikvm(811):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
08-18 14:06:11.948:E / AndroidRuntime(811):致命异常:主要
08-18 14:06:11.948:E / AndroidRuntime(811):java.lang.RuntimeException的:无法启动的活动ComponentInfo {radu.soundSampler / radu.soundSampler.DisplayMessageActivity}:java.lang.IllegalStateException
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.os.Looper.loop(Looper.java:123)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread.main(ActivityThread.java:3683)
08-18 14:06:11.948:E / AndroidRuntime(811):在java.lang.reflect.Method.invokeNative(本机方法)
08-18 14:06:11.948:E / AndroidRuntime(811):在java.lang.reflect.Method.invoke(Method.java:507)
08-18 14:06:11.948:E / AndroidRuntime(811):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 14:06:11.948:E / AndroidRuntime(811):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 14:06:11.948:E / AndroidRuntime(811):在dalvik.system.NativeStart.main(本机方法)
08-18 14:06:11.948:E / AndroidRuntime(811):致:java.lang.IllegalStateException
08-18 14:06:11.948:E / AndroidRuntime(811):在android.media.MediaRecorder.start(本机方法)
08-18 14:06:11.948:E / AndroidRuntime(811):在radu.soundSampler.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:30)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 14:06:11.948:E / AndroidRuntime(811):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-18 14:06:11.948:E / AndroidRuntime(811):11 ...更多
08-18 14:06:14.181:I /处理(811):发送信号。 PID:811 SIG:9
 

解决方案

如果您只是想记录麦克风的量,那么你可以将输出重定向到,像这样的:

 记录=新MediaRecorder();
//下面的调用抛出非法状态异常,但在这里,我们按照正确的顺序

recorder.setAudioSource(AudioSource.MIC);

//设置后音频信号源
recorder.setOutputFormat(OutputFormat.THREE_GPP);

//设置后输出格式
recorder.setAudioEn codeR(AudioEn coder.AMR_NB);
recorder.setOutputFile(的/ dev / null的);
//在prepare

尝试 {
    。录音机prepare();
}赶上(IOException异常E){
    通信System.err.println(E);
}
//之后prepare
recorder.start();

//调用getMaxAmplitude()与返回值为0,这样你就可以在以后回忆起
recorder.getMaxAmplitude();
 

此外,你应该不会让里面的onCreate 假的延迟。使用一个处理程序,而不是:

 公共无效的onCreate(包savedInstanceState){

    // ...

    处理程序=新的处理程序();
    handler.postDelayed(这一点,1000);
}

公共无效的run(){
    int值= recorder.getMaxAmplitude();
    recorder.stop();

    // 等等
}
 

I'm trying to make an app that records sound for a second and then reads the maximum amplitude from the recorded sound. This is what I have so far but my app crashes and I can't figure out why.

This is my second activity, which I'm calling from my first after I press a "Record" button. The app crashes on my emulator and on my phone.

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);
}

}

The (updated) error log on my computer says:

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

so I've thought maybe the emulator can't "run" a microphone, so I installed the .apk on my phone but it crashed there too. I don't know what could be the problem, so could you please help me, and/or give me any hints?

Thanks a lot in advance!

One more stack trace, the one you requested. I used in the catch statements o.printStackTrace() and e.printStackTrace(), I hope it's what you requested me to do:

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

解决方案

If you simply want to record the "volume" of the microphone, then you could redirect output to null, like this:

recorder = new MediaRecorder();
// following calls throw Illegal State Exceptions, but here we follow the proper order

recorder.setAudioSource(AudioSource.MIC);

// After set audio source
recorder.setOutputFormat(OutputFormat.THREE_GPP);

// After set output format
recorder.setAudioEncoder(AudioEncoder.AMR_NB);
recorder.setOutputFile("/dev/null");
// Before prepare

try {
    recorder.prepare();
} catch (IOException e) {
    System.err.println(e);
}
// After prepare
recorder.start();

// Call getMaxAmplitude() with a zero returned value, so you can recall later
recorder.getMaxAmplitude();

Also, you shouldn't make a fake delay inside onCreate. Use a handler, instead:

public void onCreate(Bundle savedInstanceState) {

    // ...

    handler = new Handler();
    handler.postDelayed(this, 1000);
}

public void run() {
    int value = recorder.getMaxAmplitude();
    recorder.stop();

    // etc
}

这篇关于在Android中录制声音,并宣读幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆