8

我正在尝试做的事情: 我正在尝试编写一个程序,该程序从 android 麦克风(不录音)读取音频并捕获一些使用服务的音量大小。现在,我正在从我的活动向我的服务发送一个脉冲,以便快速读取声音并检查幅度的 Logcat 打印输出作为我的音量计。

我的问题: AudioRecord 的 read 方法返回 0。

我试过的: 录制完整的音频而不是使用 NullOutputStream 并没有什么不同。一些早期版本在添加一些微不足道的更改(例如 logcat 调用)后随机开始工作,然后稍后停止工作。

我的想法: 我最初认为麦克风可能正在被另一个应用程序使用,但即使这是唯一运行的值得注意的服务,它仍然返回 0。

我的服务:

import org.apache.commons.io.output.NullOutputStream;


public class RecordingService extends Service {

public static final int SAMPLE_RATE = 16000;

private AudioRecord mRecorder;
private File mRecording;
private short[] mBuffer;

public static final NullOutputStream NULL_OUTPUT_STREAM = new NullOutputStream();

public double amplitude = 0.0;

public String TAG = "TAG";

public void onCreate() {
    super.onCreate();
}

public int onStartCommand(Intent intent, int flags, int startId){

    initRecorder();
    mRecorder.startRecording();
    mRecording = getFile("raw");
    startBufferedWrite(mRecording, intent);
    mRecorder.stop();
    mRecorder.release();
    stopSelf();

    return START_STICKY;
}

private void initRecorder() {
    int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT);
    mBuffer = new short[bufferSize];
    mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize);
}

private void startBufferedWrite(final File file, Intent intent) {
    Log.i(TAG, "WRITING");
    new Thread(new Runnable() {
        @Override
        public void run() {
            DataOutputStream output = null;
            Log.i(TAG, "running");
            try {
                output = new DataOutputStream(NULL_OUTPUT_STREAM);
                Log.i(TAG, "outputset");
                double sum = 0;
                //problems!
                Log.i(TAG, "mBufferlength= " + mBuffer.length);
                int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);
                Log.i(TAG, "readSize1= " + readSize);
                Log.i(TAG, mBuffer.toString());
                //problems!
                Log.i(TAG, "read");
                for (int i = 0; i < readSize; i++) {
                    output.writeShort(mBuffer[i]);
                    sum += mBuffer[i] * mBuffer[i];
                }
                Log.i(TAG, "summed up");
                if (readSize > 0) {
                    Log.i(TAG, "readSize2= "+readSize);

                    Log.i(TAG, "setting progress");
                    amplitude = sum / readSize;
                    Log.i(TAG, "amplitude= " + amplitude);
                    Log.i(TAG, "sqrt= " + Math.sqrt(amplitude));

                }
                else {
                    Log.i(TAG, "readsize <= 0");

                }

            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            } finally {
                if (output != null) {
                    try {
                        output.flush();
                    } catch (IOException e) {
                        Log.e(TAG, e.getMessage());

                    } finally {
                        try {
                            output.close();
                        } catch (IOException e) {
                            Log.e(TAG, e.getMessage());
                        }
                    }
                }
            }
        }
    }).start();
}

private File getFile(final String suffix) {
    Time time = new Time();
    time.setToNow();
    return new File(Environment.getExternalStorageDirectory(), time.format("%Y%m%d%H%M%S") + "." + suffix);
}

public void onDestroy() {
    super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
}

我的活动:

public class RecordingActivity extends Activity {

private final String startRecordingLabel = "Start recording";

public String TAG = "TAG";

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button button = (Button) findViewById(R.id.button);
    button.setText(startRecordingLabel);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
                Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
                Toast.makeText(RecordingActivity.this, "started", Toast.LENGTH_SHORT).show();
                RecordingActivity.this.startService(intent);                
                }

    });
}


@Override
public void onDestroy() {
    super.onDestroy();
}

}

这是我的第一篇文章,但我希望它很容易理解。谢谢!

4

0 回答 0