Android上的频率检测 - AudioRecord [英] Frequency detection on Android - AudioRecord

查看:401
本文介绍了Android上的频率检测 - AudioRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第二个问题,因为我HAWE一个问题。我已经在Android上导入一个简单的频率检测。对于DFT我发现jTransform库(https://sites.google.com/site/piotrwendykier/software/jtransforms)。

在jTransform的API <一个href=\"http://incanter.org/docs/parallelcolt/api/edu/emory/mathcs/jtransforms/fft/DoubleFFT_1D.html#complexForward%28double%5B%5D%29\" rel=\"nofollow\">http://incanter.org/docs/parallelcolt/api/edu/emory/mathcs/jtransforms/fft/DoubleFFT_1D.html#complexForward%28double%5B%5D%29我看到了,我已经给在complexForward()方法传递一个双矢量,东北角不是一个文件。

所以,为先,这是正确的我的想法?所以,我无法使用MediaRecorder,因为它生成一个文件?我需要一个缓冲区,然后我用AudioRecorder。

但这里有一个问题:我不能没有一个配置初始化AudioRecorder对象

的findAudioRecord()尝试所有可能的配置,以初始化AudioRecord并且在第一工作配置的方法结束(退回)。但它结束,返回null。

真的,我无法理解的问题。这有可能是我的电话?

感谢您的帮助!和对不起我的英语...

 进口android.app.Activity;
进口android.media.AudioFormat;
进口android.media.AudioManager;
进口android.media.AudioRecord;
进口android.media.AudioTrack;
进口android.media.MediaRecorder;
进口android.media.MediaRecorder.AudioSource;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;公共类FFTActivity延伸活动{私人TextView的textView1;
  私人TextView的textView2;  私有静态最终诠释RECORDSTATE_RECORDING = 3;
  私有静态最终诠释RECORDER_BPP = 16;
  私有静态最后弦乐AUDIO_RECORDER_FILE_EXT_WAV =.WAV;
  私有静态最后弦乐AUDIO_RECORDER_FOLDER =AudioRecorder;
  私有静态最后弦乐AUDIO_RECORDER_TEMP_FILE =record_temp.raw;
  私有静态最终诠释RECORDER_SAMPLERATE = 44100;
  私有静态最终诠释RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
  私有静态最终诠释RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;  私有静态诠释[] = mSampleRates新INT [] {8000,11025,22050,44100};
  私人INT缓冲区大小= 0;
  私人AudioRecord记录= NULL;  私人诠释续;  INT CONT2 = 0;
  INT contENCODING = 0;
  INT contCHANNEL = 0;  字符串s =;  / **当第一次创建活动调用。 * /
  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
  super.onCreate(savedInstanceState);
     的setContentView(R.layout.main);      textView1 =(的TextView)findViewById(R.id.testo1);
      textView2 =(的TextView)findViewById(R.id.testo2);      BUFFERSIZE = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);      录音机= findAudioRecord();}  公共AudioRecord findAudioRecord(){
        对于(INT率:mSampleRates){            对于(短AudioFormat的:新的短[] {AudioFormat.ENCODING_PCM_8BIT,AudioFormat.ENCODING_PCM_16BIT}){
            对于(短channelConfig:新的短[] {AudioFormat.CHANNEL_IN_MONO,AudioFormat.CHANNEL_IN_STEREO}){
                尝试{
                    INT缓冲区大小= AudioRecord.getMinBufferSize(速率,channelConfig,AudioFormat的);                    如果(缓冲区大小!= AudioRecord.ERROR_BAD_VALUE){
                        //检查是否我们可以实例,有一个成功
                        AudioRecord记录=新AudioRecord(AudioSource.DEFAULT,速度,channelConfig,AudioFormat的,(缓冲区大小* 2));
                        textView2.setText(NOT ERROR_BAD_VALUE+ mSampleRates [CONT2] ++缓冲区大小);                        如果(recorder.getState()== AudioRecord.STATE_INITIALIZED){                            textView2.setText(的getState():+ recorder.getState()+getMinBufferSize():+ AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING));                            返回记录;
                        }
                    }
                }赶上(例外五){
                    textView2.setText(ECCEZZIONE+ E);
                }
                contCHANNEL ++;
            }
            contENCODING ++;
        }
        CONT2 ++;
    }
    textView1.setText(非INIZIALIZZATO+ contCHANNEL ++ contENCODING);
    返回null;
}


解决方案

将以下权限使用许可权的android:NAME =android.permission.RECORD_AUDIO 之外应用标签

This is my second question because i hawe a problem. I've to implement a simple Frequency detection on Android. For the DFT i've found the jTransform library (https://sites.google.com/site/piotrwendykier/software/jtransforms).

In the API of jTransform http://incanter.org/docs/parallelcolt/api/edu/emory/mathcs/jtransforms/fft/DoubleFFT_1D.html#complexForward%28double%5B%5D%29 i had seen that i've to pass a double vector at the complexForward() method, and obliviously not a File.

So, for first, it's correct what I thought? And so, that i can't use the MediaRecorder, because it generate a File? I need a buffer, then i use AudioRecorder.

But here's the problem: i can't initialize the AudioRecorder Object with no one configuration.

The findAudioRecord() try all possible configurations to initialized the AudioRecord and at the first working configurations the methods end (for the return). But it end, and return null.

Really, i can't understand the problem. It's possible that is my phone?

Thanks for your help! and sorry for my english...

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioSource;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class FFTActivity extends Activity {

private TextView textView1;
  private TextView textView2;

  private static final int RECORDSTATE_RECORDING = 3;
  private static final int RECORDER_BPP = 16;
  private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
  private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
  private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
  private static final int RECORDER_SAMPLERATE = 44100;
  private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
  private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

  private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };


  private int bufferSize = 0;
  private AudioRecord recorder = null;

  private int cont;

  int cont2 = 0;
  int contENCODING = 0;
  int contCHANNEL = 0;

  String s ="";



  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

      textView1 = (TextView) findViewById(R.id.testo1);
      textView2 = (TextView) findViewById(R.id.testo2);

      bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);

      recorder = findAudioRecord();



}

  public AudioRecord findAudioRecord() {
        for (int rate : mSampleRates) {

            for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT,             AudioFormat.ENCODING_PCM_16BIT }) {
            for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {
                try {
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, (bufferSize*2));
                        textView2.setText("NOT ERROR_BAD_VALUE    "+mSampleRates[cont2] +"  "+ bufferSize);

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED){

                            textView2.setText("getState(): "+recorder.getState()+"   getMinBufferSize(): "+AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING));

                            return recorder;
                        }    
                    }
                } catch (Exception e) {
                    textView2.setText("ECCEZZIONE"+e);
                }
                contCHANNEL++;
            }
            contENCODING++;
        }
        cont2++;  
    }
    textView1.setText("NON INIZIALIZZATO " + contCHANNEL + "   " + contENCODING); 
    return null;
} 

解决方案

Put following permission uses-permission android:name="android.permission.RECORD_AUDIO outside application tag

这篇关于Android上的频率检测 - AudioRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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