Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过录音机感知声音的幅度?_Java_Android_Eclipse_Audio - Fatal编程技术网

Java 如何通过录音机感知声音的幅度?

Java 如何通过录音机感知声音的幅度?,java,android,eclipse,audio,Java,Android,Eclipse,Audio,我已经创建了一个示例项目,希望检查我是否可以读取声音振幅。到目前为止,我已经从主要活动开始了一项服务,在服务类中,我放了一些代码来读取声音幅度。根据教程,它应该已经工作,但不知道为什么不工作。这是密码 public class MyService extends Service { int sampleRate = 8000; AudioRecord audioRecord; int bufferSize; double las

我已经创建了一个示例项目,希望检查我是否可以读取声音振幅。到目前为止,我已经从主要活动开始了一项服务,在服务类中,我放了一些代码来读取声音幅度。根据教程,它应该已经工作,但不知道为什么不工作。这是密码

 public class MyService extends Service {

        int sampleRate = 8000;
        AudioRecord audioRecord;
        int bufferSize;
        double lastLevel;

        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("TAG", "oncreate called of service");

        }

        @Override
        public void onDestroy() {

            super.onDestroy();
            Log.d("TAG", "oncDestroy called of service");

        }



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

            Log.d("TAG", "onstart command  called of service");
            try {
                bufferSize = AudioRecord
                        .getMinBufferSize(sampleRate, AudioFormat.CHANNEL_IN_MONO,
                                AudioFormat.ENCODING_PCM_16BIT);
                audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                        sampleRate, AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_PCM_16BIT, bufferSize);
            } catch (Exception e) {
                android.util.Log.e("TrackingFlow", "Exception", e);
            }


            audioRecord.startRecording();

            if (lastLevel > 0 && lastLevel <= 50) {
                Log.d("TAG", "lastlevel is less then 50");
            } else if (lastLevel > 50 && lastLevel <= 100) {
                Log.d("TAG", "lastlevel is less then/equal 100");
            } else if (lastLevel > 100 && lastLevel <= 170) {
                Log.d("TAG", "lastlevel is less then/= 170");
            }
            if (lastLevel > 170) {
                Log.d("TAG", "lastlevel is greater then 170");
            }

            return super.onStartCommand(intent, flags, startId);

        }

        @Override
        public IBinder onBind(Intent arg0) {

            return null;

        }

    }
公共类MyService扩展服务{
int-sampleRate=8000;
录音;
int缓冲区大小;
双水平;
@凌驾
public void onCreate(){
super.onCreate();
Log.d(“TAG”,“oncreate调用服务”);
}
@凌驾
公共空间{
super.ondestory();
Log.d(“标记”、“服务的名称”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
d(“TAG”,“onstart命令调用服务”);
试一试{
缓冲区大小=录音
.getMinBufferSize(采样器、AudioFormat.CHANNEL\u单声道、,
音频格式。编码(PCM(16位);
音频记录=新的音频记录(MediaRecorder.AudioSource.MIC、,
单声道中的AudioFormat.CHANNEL\u采样器,
AudioFormat.ENCODING_PCM_16位,缓冲区大小);
}捕获(例外e){
android.util.Log.e(“TrackingFlow”,“Exception”,e);
}
录音。开始录制();
如果(lastLevel>0&&lastLevel 50&&lastLevel 100&&lastLevel 170){
Log.d(“标记”,“lastlevel大于170”);
}
返回super.onStartCommand(intent、flags、startId);
}
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
}

是教程链接。我对服务也不熟悉,我希望我的服务级别与应有的完全一致。我第一次能感觉到声音,但在读了单词之后,就感觉不到声音的其余部分了。我的服务级别不对吗?或者录音机出了什么问题,我想不出来。我已经调试它太第一次它的工作,但在那之后不工作

到底是什么不起作用了???我的服务类叫audiorecorder,也是start,但它不能感知Ampplitue,也不能调用我在onstartcommand功能中创建的日志。你应该用你的debugger@zerO我在调试后编辑了我的问题,我能第一次感觉到声音,但在那之后它就消失了不工作。