2015-08-15 4 views
1

Я пытаюсь создать Android-плагин для телефонного звонка, который начинает записывать шум, а когда пользователь начинает говорить, останавливает запись шума и запускает новую запись для голоса.Phonegap plugin record audio

Я смог заставить его работать в Android, но когда я пытаюсь добавить его в качестве плагина для телефонной книги, он не работает.

Код выглядит следующим образом:

RecordVoice.js

var RecordVoice = { 
startRecord: function(successCallback, errorCallback) { 
    cordova.exec(
     successCallback, // success callback function 
     errorCallback, // error callback function 
     'RecordVoice', // mapped to our native Java class called  "RecordVoicePlugin" 
     'startrecording'); 
} 
} 
module.exports = RecordVoice; 

RecordVoice.java

package org.maria.RecordVoice; 
    import org.apache.cordova.CallbackContext; 
    import org.apache.cordova.CordovaPlugin; 
    import org.json.JSONObject; 
    import org.json.JSONArray; 
    import org.json.JSONException; 

    import android.app.Activity; 
    import android.content.Intent; 
    import com.androidexample.noisealert.R; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.media.MediaRecorder; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.os.Handler; 
    import android.os.PowerManager; 
    import android.util.Log; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    import java.io.IOException; 

    public class NoiseAlert extends CordovaPlugin { 
    public static final String ACTION_ADD_RECORD_ENTRY = "startrecording"; 
     /* constants */ 
      private static final int POLL_INTERVAL = 300; 

      /** running state **/ 
      private boolean mRunning = false; 

      /** config state **/ 
      private int mThreshold; 

      private PowerManager.WakeLock mWakeLock; 

      private Handler mHandler = new Handler(); 

      /* References to view elements */ 
      private TextView mStatusView; 
      private SoundLevelView mDisplay; 

      /* sound data source */ 

     private boolean thesholdReached =false; 
     @Override 
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
      try { 
       if (ACTION_ADD_RECORD_ENTRY.equals(action)) { 
        JSONObject arg_object = args.getJSONObject(0); 

       cordova.getActivity().runOnUiThread(new Runnable() { 
        public void run() { 
          double amp = getAmplitude(); 

          //Log.i("Noise", "runnable mPollTask"); 
          updateDisplay(amp, amp); 

          if ((amp > mThreshold)) { 
            //start(); 

           if(thesholdReached==false) { 
            stop(); 
            startrecordingvoice(); 
            // Show alert when noise thersold crossed 
            Toast.makeText(getApplicationContext(), "Noise Thersold Crossed, do here your stuff.", 
              Toast.LENGTH_LONG).show(); 
            //callForHelp(); 
            //Log.i("Noise", "==== onCreate ==="); 
            thesholdReached=true; 
           } 
          } 

         callbackContext.success(); 
        } 
       } 

        return true; 
       } 
       callbackContext.error("Invalid action"); 
       return false; 
      } catch(Exception e) { 
       System.err.println("Exception: " + e.getMessage()); 
       callbackContext.error(e.getMessage()); 
       return false; 
      } 
     } 


      private void start() { 

        startrecordingnoise(); 
        if (!mWakeLock.isHeld()) { 
          mWakeLock.acquire(); 
        } 

        //Noise monitoring start 
        // Runnable(mPollTask) will execute after POLL_INTERVAL 
        mHandler.postDelayed(mPollTask, POLL_INTERVAL); 
      } 

      private void stop() { 
       Log.i("Noise", "==== Stop Noise Monitoring==="); 
        if (mWakeLock.isHeld()) { 
          mWakeLock.release(); 
        } 
        mHandler.removeCallbacks(mSleepTask); 
        mHandler.removeCallbacks(mPollTask); 

       if (mRecorder != null) { 
        mRecorder.stop(); 
        mRecorder.release(); 
        mRecorder = null; 
       } 
        mDisplay.setLevel(0,0); 

        mRunning = false; 

      } 


      private void initializeApplicationConstants() { 
        // Set Noise Threshold 
        mThreshold = 8; 

      } 

      private void updateDisplay(double ampl, double signalEMA) { 
        mStatusView.setText(Double.toString(ampl)); 
        // 
        mDisplay.setLevel((int)signalEMA, mThreshold); 
      } 


      private void callForHelp() { 

       stop(); 
       startrecordingvoice(); 
       // Show alert when noise thersold crossed 
        Toast.makeText(getApplicationContext(), "Noise Thersold Crossed, do here your stuff.", 
          Toast.LENGTH_LONG).show(); 

      } 
     // This file is used to record voice 
     static final private double EMA_FILTER = 0.6; 

     private MediaRecorder mRecorder = null; 
     private double mEMA = 0.0; 

     public void startrecordingnoise() { 

      if (mRecorder == null) { 
       String outputFile = null; 
       outputFile = Environment.getExternalStorageDirectory() + "/NoiseRecording.amr"; 

       mRecorder = new MediaRecorder(); 
       mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
       mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
       mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
       //mRecorder.setOutputFile("/dev/null"); 
       mRecorder.setOutputFile(outputFile); 

       try { 
        mRecorder.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       mRecorder.start(); 
       mEMA = 0.0; 
      } 
     } 
     public void startrecordingvoice() { 

      if (mRecorder == null) { 
       String outputFile = null; 
       outputFile = Environment.getExternalStorageDirectory() + "/VoiceRecording.amr"; 

       mRecorder = new MediaRecorder(); 
       mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
       mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
       mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
       //mRecorder.setOutputFile("/dev/null"); 
       mRecorder.setOutputFile(outputFile); 

       try { 
        mRecorder.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       mRecorder.start(); 
       mEMA = 0.0; 
      } 
     } 


     public double getAmplitude() { 
      if (mRecorder != null) 
       return (mRecorder.getMaxAmplitude()/2700.0); 
      else 
       return 0; 

     } 

     public double getAmplitudeEMA() { 
      double amp = getAmplitude(); 
      mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA; 
      return mEMA; 
     } 

    }; 

index.js

  var app = { 
       // Application Constructor 
       initialize: function() { 
        this.bindEvents(); 
       }, 
       // Bind Event Listeners 
       // 
       // Bind any events that are required on startup. Common events are: 
       // 'load', 'deviceready', 'offline', and 'online'. 
       bindEvents: function() { 
        document.addEventListener('deviceready', this.onDeviceReady, false); 
       }, 
       // deviceready Event Handler 
       // 
       // The scope of 'this' is the event. In order to call the 'receivedEvent' 
       // function, we must explicitly call 'app.receivedEvent(...);' 
       onDeviceReady: function() { 
        app.receivedEvent('deviceready'); 
        app.addToCal(); 
       }, 
       // Update DOM on a Received Event 
       receivedEvent: function(id) { 
        var parentElement = document.getElementById(id); 
        var listeningElement = parentElement.querySelector('.listening'); 
        var receivedElement = parentElement.querySelector('.received'); 

        listeningElement.setAttribute('style', 'display:none;'); 
        receivedElement.setAttribute('style', 'display:block;'); 

        console.log('Received Event: ' + id); 
       }, 
      addToCal: function() { 

        var success = function() { alert("Success"); }; 
        var error = function(message) { alert("Oopsie! " + message); }; 
        RecordVoice.startRecord(success, error); 
      } 
      }; 

plugin.xml

<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0" 
     id="org.maria.RecordVoice" 
    version="0.1.0"> 
<name>RecordVoice</name> 
<description>Sample PhoneGap RecordVoice Plugin</description> 
<license>MIT</license> 
<keywords>phonegap,RecordVoice</keywords> 


<js-module src="www/RecordVoice.js" name="RecordVoice"> 
    <clobbers target="window.RecordVoice" /> 
</js-module> 

<!-- android --> 
<platform name="android"> 
    <config-file target="res/xml/config.xml" parent="/*"> 
     <feature name="RecordVoice"> 
      <param name="android-package" value="org.maria.RecordVoice.RecordVoice"/> 
     </feature> 
    </config-file> 

    <source-file src="src/android/RecordVoice.java" target-dir="src/org/maria/RecordVoice" />  
</platform>   

Любая идея, почему это не работает? Большое вам спасибо!

С уважением!

ответ

0

В вашем RecordVoice.js файла попытаться добавить пустой массив [], чтобы запускаемая функция:

var RecordVoice = { 
startRecord: function(successCallback, errorCallback) { 
cordova.exec(
    successCallback, // success callback function 
    errorCallback, // error callback function 
    'RecordVoice', // mapped to our native Java class called "RecordVoicePlugin" 
    'startrecording', 
    [] 
); 
} 
} 
module.exports = RecordVoice;