List of usage examples for android.media AudioRecord RECORDSTATE_STOPPED
int RECORDSTATE_STOPPED
To view the source code for android.media AudioRecord RECORDSTATE_STOPPED.
Click Source Link
From source file:edu.gvsu.masl.echoprint.AudioFingerprinter.java
/** * The main thread<br>/* ww w .j a v a 2 s. c o m*/ * Records audio and generates the audio fingerprint, then it queries the server for a match and forwards the results to the listener. */ public void run() { this.isRunning = true; try { // create the audio buffer // get the minimum buffer size int minBufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL, ENCODING); // and the actual buffer size for the audio to record // frequency * seconds to record. bufferSize = Math.max(minBufferSize, this.FREQUENCY * this.secondsToRecord); audioData = new short[bufferSize]; // start recorder mRecordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL, ENCODING, minBufferSize); willStartListening(); mRecordInstance.startRecording(); boolean firstRun = true; do { try { willStartListeningPass(); long time = System.currentTimeMillis(); // fill audio buffer with mic data. int samplesIn = 0; do { samplesIn += mRecordInstance.read(audioData, samplesIn, bufferSize - samplesIn); if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) break; } while (samplesIn < bufferSize); Log.d("Fingerprinter", "Audio recorded: " + (System.currentTimeMillis() - time) + " millis"); // see if the process was stopped. if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED || (!firstRun && !this.continuous)) break; // create an echoprint codegen wrapper and get the code time = System.currentTimeMillis(); Codegen codegen = new Codegen(); String code = codegen.generate(audioData, samplesIn); Log.d("Fingerprinter", "Codegen created in: " + (System.currentTimeMillis() - time) + " millis"); if (code.length() == 0) { // no code? // not enough audio data? continue; } didGenerateFingerprintCode(code); // fetch data from echonest time = System.currentTimeMillis(); String urlstr = SERVER_URL + code; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(urlstr); // get response HttpResponse response = client.execute(get); // Examine the response status Log.d("Fingerprinter", response.getStatusLine().toString()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release String result = ""; if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); result = convertStreamToString(instream); // now you have the string representation of the HTML request instream.close(); } Log.d("Fingerprinter", "Results fetched in: " + (System.currentTimeMillis() - time) + " millis"); // parse JSON JSONObject jobj = new JSONObject(result); if (jobj.has("code")) Log.d("Fingerprinter", "Response code:" + jobj.getInt("code") + " (" + this.messageForCode(jobj.getInt("code")) + ")"); if (jobj.has("match")) { if (jobj.getBoolean("match")) { Hashtable<String, String> match = new Hashtable<String, String>(); match.put(SCORE_KEY, jobj.getDouble(SCORE_KEY) + ""); match.put(TRACK_ID_KEY, jobj.getString(TRACK_ID_KEY)); // the metadata dictionary IS NOT included by default in the API demo server // replace line 66/67 in API.py with: // return json.dumps({"ok":True,"message":response.message(), "match":response.match(), "score":response.score, \ // "qtime":response.qtime, "track_id":response.TRID, "total_time":response.total_time, "metadata":response.metadata}) if (jobj.has("metadata")) { JSONObject metadata = jobj.getJSONObject("metadata"); if (metadata.has(SCORE_KEY)) match.put(META_SCORE_KEY, metadata.getDouble(SCORE_KEY) + ""); if (metadata.has(TITLE_KEY)) match.put(TITLE_KEY, metadata.getString(TITLE_KEY)); if (metadata.has(ARTIST_KEY)) match.put(ARTIST_KEY, metadata.getString(ARTIST_KEY)); if (metadata.has(ALBUM_KEY)) match.put(ALBUM_KEY, metadata.getString(ALBUM_KEY)); } didFindMatchForCode(match, code); } else didNotFindMatchForCode(code); } else { didFailWithException(new Exception("Unknown error")); } firstRun = false; didFinishListeningPass(); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } } while (this.continuous); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } if (mRecordInstance != null) { mRecordInstance.stop(); mRecordInstance.release(); mRecordInstance = null; } this.isRunning = false; didFinishListening(); }
From source file:com.example.echoprint.AudioFingerprinter.java
/** * The main thread<br>// w w w . j av a2 s . c o m * Records audio and generates the audio fingerprint, then it queries the server for a match and forwards the results to the listener. */ public void run() { this.isRunning = true; try { // create the audio buffer // get the minimum buffer size int minBufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL, ENCODING); // and the actual buffer size for the audio to record // frequency * seconds to record. bufferSize = Math.max(minBufferSize, this.FREQUENCY * this.secondsToRecord); audioData = new short[bufferSize]; // start recorder mRecordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL, ENCODING, minBufferSize); willStartListening(); mRecordInstance.startRecording(); boolean firstRun = true; do { try { willStartListeningPass(); long time = System.currentTimeMillis(); // fill audio buffer with mic data. int samplesIn = 0; do { samplesIn += mRecordInstance.read(audioData, samplesIn, bufferSize - samplesIn); if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) break; } while (samplesIn < bufferSize); Log.d("Fingerprinter", "Audio recorded: " + (System.currentTimeMillis() - time) + " millis"); // see if the process was stopped. if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED || (!firstRun && !this.continuous)) break; // create an echoprint codegen wrapper and get the code time = System.currentTimeMillis(); Codegen codegen = new Codegen(); String code = codegen.generate(audioData, samplesIn); Log.d("Fingerprinter", "Codegen created in: " + (System.currentTimeMillis() - time) + " millis"); if (code.length() == 0) { // no code? // not enough audio data? continue; } didGenerateFingerprintCode(code); // fetch data from echonest time = System.currentTimeMillis(); String urlstr = SERVER_URL + code; //String urlstr = "http://developer.echonest.com/api/v4/song/identify?api_key=GUYHX8VIYSCI79EZI&code="; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(urlstr); // get response HttpResponse response = client.execute(get); // Examine the response status Log.d("Fingerprinter", response.getStatusLine().toString()); // Get hold of the response entity HttpEntity entity = response.getEntity(); // If the response does not enclose an entity, there is no need // to worry about connection release String result = ""; if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); result = convertStreamToString(instream); // now you have the string representation of the HTML request instream.close(); } Log.d("Fingerprinter", "Results fetched in: " + (System.currentTimeMillis() - time) + " millis"); // parse JSON JSONObject jobj = new JSONObject(result); if (jobj.has("code")) Log.d("Fingerprinter", "Response code:" + jobj.getInt("code") + " (" + this.messageForCode(jobj.getInt("code")) + ")"); if (jobj.has("match")) { if (jobj.getBoolean("match")) { Hashtable<String, String> match = new Hashtable<String, String>(); match.put(SCORE_KEY, jobj.getDouble(SCORE_KEY) + ""); match.put(TRACK_ID_KEY, jobj.getString(TRACK_ID_KEY)); // the metadata dictionary IS NOT included by default in the API demo server // replace line 66/67 in API.py with: // return json.dumps({"ok":True,"message":response.message(), "match":response.match(), "score":response.score, \ // "qtime":response.qtime, "track_id":response.TRID, "total_time":response.total_time, "metadata":response.metadata}) if (jobj.has("metadata")) { JSONObject metadata = jobj.getJSONObject("metadata"); if (metadata.has(SCORE_KEY)) match.put(META_SCORE_KEY, metadata.getDouble(SCORE_KEY) + ""); if (metadata.has(TITLE_KEY)) match.put(TITLE_KEY, metadata.getString(TITLE_KEY)); if (metadata.has(ARTIST_KEY)) match.put(ARTIST_KEY, metadata.getString(ARTIST_KEY)); if (metadata.has(ALBUM_KEY)) match.put(ALBUM_KEY, metadata.getString(ALBUM_KEY)); } didFindMatchForCode(match, code); } else didNotFindMatchForCode(code); } else { didFailWithException(new Exception("Unknown error")); } firstRun = false; didFinishListeningPass(); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } } while (this.continuous); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } if (mRecordInstance != null) { mRecordInstance.stop(); mRecordInstance.release(); mRecordInstance = null; } this.isRunning = false; didFinishListening(); }
From source file:com.xzg.fingerprinter.AudioFingerprinter.java
/** * The main thread<br>/*from w w w . j a v a 2 s .c om*/ * Records audio and generates the audio fingerprint, then it queries the * server for a match and forwards the results to the listener. */ public void run() { this.isRunning = true; try { // create the audio buffer // get the minimum buffer size int minBufferSize = AudioRecord.getMinBufferSize(FREQUENCY, CHANNEL, ENCODING); System.out.println("minBufferSize: " + minBufferSize); // and the actual buffer size for the audio to record // frequency * seconds to record. bufferSize = Math.max(minBufferSize, this.FREQUENCY * this.secondsToRecord); System.out.println("BufferSize: " + bufferSize); audioData = new byte[bufferSize * 2]; // start recorder mRecordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, FREQUENCY, CHANNEL, ENCODING, minBufferSize); willStartListening(); mRecordInstance.startRecording(); boolean firstRun = true; do { try { willStartListeningPass(); long time = System.currentTimeMillis(); // fill audio buffer with mic data. int samplesIn = 0; do { samplesIn += mRecordInstance.read(audioData, samplesIn, bufferSize - samplesIn); if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) break; } while (samplesIn < bufferSize); Log.d("Fingerprinter", "Audio recorded: " + (System.currentTimeMillis() - time) + " millis"); // see if the process was stopped. if (mRecordInstance.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED || (!firstRun && !this.continuous)) break; Log.d("Fingerprinter", "Recod state: " + mRecordInstance.getRecordingState()); byte[] audioDataByteFormat = (audioData); Wave w = new Wave(); w.data = audioDataByteFormat; WaveFileManager wm = new WaveFileManager(); wm.setWave(w); wm.saveWaveAsFile("/sdcard/xzgrecord.wav"); Clip c = Clip.newInstance(audioDataByteFormat, this.FREQUENCY); // create an echoprint codegen wrapper and get the code time = System.currentTimeMillis(); Codegen codegen = new Codegen(c); String code = codegen.genCode(); // Log.d("Fingerprinter","codegen before"); // String code = codegen.generate(audioData, samplesIn); Log.d("Fingerprinter", "codegen after"); Log.d("Fingerprinter", "Codegen created in: " + (System.currentTimeMillis() - time) + " millis"); // Log.d("Fingerprinter", "code length is " + code.length()); if (code.length() == 0) { // no code? // not enough audio data? continue; } // fetch data from echonest long startTime = System.currentTimeMillis(); String result = fetchServerResult(code); long endTime = System.currentTimeMillis(); long fetchTime = endTime - startTime; Log.d("Fingerprinter", "Results fetched in: " + (fetchTime) + " millis"); Log.d("Fingerprinter", "HTTP result: " + result); // parse JSON JSONObject jsonResult = new JSONObject(result); if (jsonResult.has("id")) Log.d("Fingerprinter", "Response code:" + jsonResult.getInt("id")); if (jsonResult.has("id")) { if (jsonResult.getInt("id") >= 0) { Hashtable<String, String> match = parseResult(jsonResult); didFindMatchForCode(match, code); } else didNotFindMatchForCode(code); } else { didFailWithException(new Exception("Unknown error")); } // firstRun = false; didFinishListeningPass(); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } } while (this.continuous); } catch (Exception e) { e.printStackTrace(); Log.e("Fingerprinter", e.getLocalizedMessage()); didFailWithException(e); } if (mRecordInstance != null) { mRecordInstance.stop(); mRecordInstance.release(); mRecordInstance = null; } this.isRunning = false; didFinishListening(); }