MediaRecorder崩溃

时间:2013-11-16 23:58:22

标签: java android mediarecorder


Android / Java非常初学者,来自C和Symbian,我从Hellworld示例开始尝试显示声级(项目是在文本文件中记录这个级别数小时)。 此代码编写在MainActivity文件中,作为MainActivity类中的方法:

public void GetAFlevel(View view) throws IllegalStateException, IOException {
    float audiolevel = 0, maxal = 0;
    MediaRecorder mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mRecorder.setOutputFile("/dev/null");
    mRecorder.prepare();
    mRecorder.start();
    for (int i = 0; i < 1000; i++) {
        audiolevel = mRecorder.getMaxAmplitude();
        if (audiolevel > maxal)
            maxal = audiolevel;
    }
    EditText editText1 = (EditText) findViewById(R.id.editText1);
    editText1.setText(String.valueOf(maxal));
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
}

结果是应用程序在目标上崩溃:

11-17 00:23:09.859: D/AndroidRuntime(22096): Shutting down VM

11-17 00:23:09.859: W/dalvikvm(22096): threadid=1: thread exiting with uncaught exception (group=0x40018578)

11-17 00:23:09.921: E/AndroidRuntime(22096): FATAL EXCEPTION: main

11-17 00:23:09.921: E/AndroidRuntime(22096): java.lang.IllegalStateException: Could not execute method of the activity

如果删除以下MediaRecorder函数,则不会崩溃:setAudioSource,setOutputFormat,setAudioEncoder,setOutputFile,prepare,start和stop,但getMaxAmplitude当然会返回0。

只要我只添加setAudioSource(通常是getMaxAmplitude唯一必需的),崩溃!

有什么想法吗?

提前致谢。


您好

终于找到了一种工作代码的方法:我把它放在OnCreate方法中:

package com.example.mfa;

import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;
import android.media.MediaRecorder;

public class MainActivity extends Activity {
    public MediaRecorder mRecorder  =null;
    public float audiolevel=0,maxal=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MediaRecorder mRecorder  = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mRecorder.setOutputFile("/dev/null");
    try {
        mRecorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mRecorder.start();
        for (int i=0;i<10000;i++){
        audiolevel=mRecorder.getMaxAmplitude ();
        if (audiolevel>maxal) maxal=audiolevel;}
        EditText editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setText(String.valueOf(maxal));   
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
    }
}

备注:setOutputFile没有问题(“/ dev / null”);
mRecorder.prepare();用try / catch包围

好吧但如果我把相同的代码放在按钮调用的其他方法中,崩溃,为什么? 如果按钮创建新活动,则相同:代码必须位于OnCreate方法或崩溃。

感谢您的帮助:需要回归JAVA的基础知识吗?

0 个答案:

没有答案