Android Open Source - CopresenceDataCollector Audio Worker






From Project

Back to project page CopresenceDataCollector.

License

The source code is released under:

Copyright (c) 2014, Xiang Gao All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Re...

If you think the Android project CopresenceDataCollector listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.sesy.coco.datacollector;
// w w w.  j  av  a  2 s . c  o m
import java.io.File;
import java.util.HashMap;

import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.sesy.coco.datacollector.audio.ExtAudioRecorder;
import org.sesy.coco.datacollector.database.Entry;
import org.sesy.coco.datacollector.file.FileHelper;
import org.sesy.coco.datacollector.log.ConfigureLog4J;

import com.google.gson.Gson;
import com.musicg.wave.Wave;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.os.SystemClock;
import android.provider.Settings.Secure;

public class AudioWorker extends Service {

  Logger log;
  private CountDownTimer timer;
  //private MediaRecorder recorder;
  private ExtAudioRecorder extAudioRecorder;
  //private Date timestamp;
  //private int gt, ob;
  private boolean ar;
  private static long ts = 0L;
  
  @Override
  public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
  }

  
  @Override
  public void onCreate(){
    super.onCreate();
    
    log = Logger.getLogger(AudioWorker.class);  
        ConfigureLog4J.configure(this);  
        LogManager.getRootLogger().setLevel((Level)Level.DEBUG);   
        //log.info("onCreate");       
        
        
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    
    WorkerService.audioTask = true;
    log.info("Subtask Audio");
    //gt = intent.getIntExtra("gt", 0);
    //ob = intent.getIntExtra("ob", 0);
    //ar = intent.getBooleanExtra("ar", false);
    ar = true;
    WorkerService.audList.clear();
    extAudioRecorder = ExtAudioRecorder.getInstanse(false);
    
    timer = new CountDownTimer(Constants.AUDIO_PERIOD * 1000, 1000) {

      @Override
      public void onFinish() {
        // TODO Auto-generated method stub
        
        log.info("Audio scan finished");
        //recorder.stop();
        //recorder.reset();
        //recorder.release();
        extAudioRecorder.stop();
        extAudioRecorder.reset();
        extAudioRecorder.release();
        
        String fp =  "####" + WorkerService.wavName;
        /*if(recordLock){
          fp = wavName + "#";
        }*/
        
        //if(ar){
          Wave wave = new Wave(WorkerService.wavPath);
          Gson gson = new Gson();
          String waveHeaderJson = gson.toJson(wave.getWaveHeader());
          String waveData = new String(Hex.encodeHex(wave.getBytes()));
          /*--------------------
           * send json string to server with "send"
           * -------------------
           */
          /*HashMap<String,String> msgObj = new HashMap<String,String>();
              String msg = ""; 
              msgObj.clear();                    
              
                msgObj.put("id", Constants.SEND);
                msgObj.put("uuid", WorkerService.uuid);
                msgObj.put("header", waveHeaderJson);
                msgObj.put("data", waveData);
                msg = gson.toJson(msgObj);
                //log.info("SEND msg built: "+msg);
              if (DaemonService.socket.isConnected()) {  
                      if (!DaemonService.socket.isOutputShutdown()) {  
                        DaemonService.out.println(msg);
                        DaemonService.out.flush();
                        DaemonService.timeoutSet = true;
                        DaemonService.timeoutTimer = SystemClock.elapsedRealtime();
                        DaemonService.timeoutCounter = 0;
                        log.info("SEND msg sent");
                      }  
                  }  
              DaemonService.taskTimeout = SystemClock.elapsedRealtime();           
              
              //remove wav if not server storage not enabled  
          if(!WorkerService.isRecord){
            new File(WorkerService.wavPath).delete();
          }*/
          
          Entry eobj = new Entry();        
          //eobj.setOB(ob);
          eobj.setTS(ts);
          //eobj.setGT(gt);
          eobj.setMT(Constants.STATUS_SENSOR_AUDIO);
          eobj.setFP(fp);
              WorkerService.audList.add(eobj);
          
          if(ar){  //if this is trigger side
            WorkerService.audioTaskDone = true;
          }
          
        //}    
        
        stopSelf();
      }

      @Override
      public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
        
      }
      
    };
    
    FileHelper fh = new FileHelper(getApplicationContext());
    String uuid = Secure.getString(getApplicationContext().getContentResolver(),Secure.ANDROID_ID);
    
    String root = fh.getDir();
    
    ts = SystemClock.elapsedRealtime() - WorkerService.metaTS;
    
    WorkerService.wavName = "test_" + String.valueOf(WorkerService.ob) + "_" +uuid + "_" + ts + ".wav";
    WorkerService.wavPath = root + "/" + WorkerService.wavName;
    //recorder.setOutputFile(WorkerService.wavPath);
    extAudioRecorder.setOutputFile(WorkerService.wavPath);
    
    try {
      //recorder.prepare();
      extAudioRecorder.prepare();
    } catch (IllegalStateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }    
    
    //recorder.start();
    extAudioRecorder.start();
    timer.start();
    
    
    return START_NOT_STICKY;    
  }
}




Java Source Code List

org.sesy.coco.datacollector.ARPWorker.java
org.sesy.coco.datacollector.AlarmService.java
org.sesy.coco.datacollector.AppLauncher.java
org.sesy.coco.datacollector.AudioProc.java
org.sesy.coco.datacollector.AudioWorker.java
org.sesy.coco.datacollector.BindActivity.java
org.sesy.coco.datacollector.BluetoothWorker.java
org.sesy.coco.datacollector.CellWorker.java
org.sesy.coco.datacollector.Constants.java
org.sesy.coco.datacollector.DaemonService.java
org.sesy.coco.datacollector.DataMonitor.java
org.sesy.coco.datacollector.GpsWorker.java
org.sesy.coco.datacollector.HelpActivity.java
org.sesy.coco.datacollector.MainActivity.java
org.sesy.coco.datacollector.MyPreference.java
org.sesy.coco.datacollector.MyWidgetProvider.java
org.sesy.coco.datacollector.PluginManager.java
org.sesy.coco.datacollector.PrefManager.java
org.sesy.coco.datacollector.ReportErrActivity.java
org.sesy.coco.datacollector.SDSetupActivity.java
org.sesy.coco.datacollector.SensorActivity.java
org.sesy.coco.datacollector.SensorListener.java
org.sesy.coco.datacollector.SensordroneWorker.java
org.sesy.coco.datacollector.SettingActivity.java
org.sesy.coco.datacollector.StatusActivity.java
org.sesy.coco.datacollector.StatusManager.java
org.sesy.coco.datacollector.TriggerService.java
org.sesy.coco.datacollector.UpdateWidgetService.java
org.sesy.coco.datacollector.WifiWorker.java
org.sesy.coco.datacollector.WorkerService.java
org.sesy.coco.datacollector.audio.Convolution.java
org.sesy.coco.datacollector.audio.CrossCorrelation.java
org.sesy.coco.datacollector.audio.ExtAudioRecorder.java
org.sesy.coco.datacollector.audio.XCorrAndDistFromWav.java
org.sesy.coco.datacollector.communication.HttpFileUploader.java
org.sesy.coco.datacollector.database.Entry.java
org.sesy.coco.datacollector.file.FileHelper.java
org.sesy.coco.datacollector.log.ConfigureLog4J.java
org.sesy.coco.datacollector.net.NetInfo.java
org.sesy.coco.datacollector.plugin.PlugInterface.java
wei.mark.standout.StandOutWindow.java
wei.mark.standout.Utils.java
wei.mark.standout.WindowCache.java
wei.mark.standout.constants.StandOutFlags.java
wei.mark.standout.ui.TouchInfo.java
wei.mark.standout.ui.Window.java