Il Forum è consultabile solo in modalità lettura. Per domande o consigli iscriviti al nostro GRUPPO FACEBOOK / COMMUNITY cliccando qui

funzione getMaxAmplitude() torna sempre 0

Discussione in 'Development' iniziata da Prying, 1 Mar 2016.

  1. Prying

    Prying Baby Droid

    Iscritto:
    29 Dic 2015
    Messaggi:
    4
    "Mi Piace":
    0
    Salve a tutti, sto cercando di sviluppare un'app in cui ci sia un servizio. Tale servizio dovrebbe ogni tot di tempo richiedere il microfono per definire proprio il volume in quell'istante, solo che a me torna sempre zero. Qualcuno riesce a spiegarmi come mai? Grazie in anticipo
    Codice:
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaRecorder;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Looper;
    import android.os.Message;
    import android.util.Log;
    
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;
    
    import java.io.IOException;
    
    import cz.msebera.android.httpclient.Header;
    
    
    public class MicrophoneService extends Service {
        String url;
        Handler h;
    
       
        @Override
        public void onCreate() {
    
            super.onCreate();
            Thread t=new Thread() {
              public void run() {
                synchronized (MicrophoneService.this) {
                    Looper.prepare();
                    h=new Handler() {
                        @Override
                        public void handleMessage(Message msg) {
                            switch (msg.what) {
                                case 1:
                                    //ottengo il microfono
                                    MediaRecorder mediaRecorder = new MediaRecorder();
                                    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                                    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                                    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                                    mediaRecorder.setOutputFile("/dev/null");
    
                                    try {
                                        mediaRecorder.prepare();
    
                                    } catch (IOException e) {
                                        throw new RuntimeException(e);
                                    }
    
                                    mediaRecorder.start();
                                   
                                    double amplitude;
                                    double db_amplitude;
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException ie) {
                                        //ignore exception
                                    }
    
                                    amplitude = mediaRecorder.getMaxAmplitude();
                                    Log.i("AMPLITUDE", new Integer((int) amplitude).toString());
                                   
                                    db_amplitude =  20.0 * Math.log10(amplitude);
                                   
                                   
                                    mediaRecorder.stop();
                                    mediaRecorder.reset();
                                    mediaRecorder.release();
                                    mediaRecorder = null;
    
                                    if (db_amplitude > 80) {
                                      // faccio qualcosa
    
                                    }
                                    //mi mando un messaggio ritardato per rifare tutto di nuovo
                                    Message m1=obtainMessage(1);
                                    sendMessageDelayed(m1, 3000);
    
                                    break;
                                case 2:
                                    getLooper().quitSafely();
                                    break;
                            }
                        }
                    };
                }
                Looper.loop();
              }
            };
            t.start();
            Handler handler=null;
            do {
                synchronized (this) {
                    handler = h;
                }
            } while (handler==null);
            handler.sendMessage(handler.obtainMessage(1));
        }
    
    
    
        @Override
        public IBinder onBind(Intent intent) {
            Log.i("onBind:", "onBind");
            return null;
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            h.sendMessage(h.obtainMessage(2));
            try {
                h.getLooper().getThread().join();
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
        }
    }