【问题标题】:Android AudioRecord.stop() leads to crashAndroid AudioRecord.stop() 导致崩溃
【发布时间】:2014-01-26 15:59:04
【问题描述】:

我正在尝试使用 AudioRecord (http://developer.android.com/reference/android/media/AudioRecord.html#stop%28%29) 在 Android 上录制音频。我适合Android AudioRecord example

我的代码如下:

import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import de.benediktbock.fft.fft;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder.AudioSource;
import java.io.IOException;


public class MainActivity extends Activity {
    private static final String LOG_TAG = "FFTTEST";
    private PlayButton   mPlayButton = null;
    private TextView    realTeil = null;
    private TextView    imgTeil = null;
    private fft         mFFT = null; 

    private int channel_config = AudioFormat.CHANNEL_IN_MONO;
    private int format = AudioFormat.ENCODING_PCM_16BIT;
    private int sampleRate = 8000;
    private int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channel_config, format);
    private AudioRecord audioInput = null; //new AudioRecord(AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
    private short[] audioBuffer = new short[bufferSize];

    private Thread readingThread = null;
    private boolean isRecording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout ll = new LinearLayout(this);
        mPlayButton = new PlayButton(this);

        ll.addView(mPlayButton,new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));

        realTeil = new TextView(this);
        ll.addView(realTeil,new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));

        imgTeil = new TextView(this);
        ll.addView(imgTeil,new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                0));
        setContentView(ll);

        realTeil.setText("Realteil");
        imgTeil.setText("Imaginärteil");

    }

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

    class PlayButton extends Button {
        boolean mStartPlaying = true;

        OnClickListener clicker = new OnClickListener() {
            public void onClick(View v) {
                onPlay(mStartPlaying);
                if (mStartPlaying) {
                    setText("Stop");
                } else {
                    setText("Start");
                }
                mStartPlaying = !mStartPlaying;
            }
        };

        public PlayButton(Context ctx) {
            super(ctx);
            setText("Start");
            setOnClickListener(clicker);
        }
    }

    private void onPlay(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }

    }

    private void startRecording()
    {
        //create and start recorder
        audioInput = new AudioRecord(AudioSource.MIC, sampleRate, channel_config, format, bufferSize);
        audioInput.startRecording();
        isRecording = true;

        //start reading thread
        readingThread = new Thread(new Runnable() 
            {
                @Override
                public void run() 
                {
                    readAudioToBuffer();
                }
            },"readAudio Thread");
        readingThread.start();
    }

    private void readAudioToBuffer()
    {
        while(isRecording)
        {
            audioInput.read(audioBuffer, 0,bufferSize);
        }
        audioInput.stop(); // on this point the app crashes (after clicking "stop")
        audioInput.release();
        audioInput = null;
    }

    private void stopRecording()
    {
        isRecording = false;
        readingThread = null;
    }
}

我可以毫无问题地开始录制。但是当我停止记录时,应用程序崩溃了。我发现它在 audioInput.stop() 行崩溃了。

有人知道问题出在哪里吗?我完全不知道。

【问题讨论】:

  • 在 logcat 输出中查找错误消息和/或异常跟踪。
  • 异常是关于在另一个线程中设置视图,而不是在其中创建视图。它似乎是 stop()。对不起

标签: java android audio audio-recording audiorecord


【解决方案1】:
private void stopRecording()
{
    isRecording = false;
   // readingThread = null;
}

设置isRecording=false后,你指定readingThread=null但线程立即无法完成他的任务。所以它会导致你的应用崩溃。

【讨论】:

  • 这会抛出什么异常?
  • 此时没有异常。线程不会以 readingThread=null 停止
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
相关资源
最近更新 更多