Recording and playing audio - University of Delaware

Recording and playing audio - University of Delaware

Recording and playing audio App Make app AudioFun with 4 buttons Start recording (id=StartRecording) Stop recording (id=StopRecording) Start playback (id=StartPlayback) Stop playback (id=StopPlayback) Include permission to Record Audio There is no permission to play on audio device. So a malicious app could play a scary noise in the middle of the night! Include three class attributes for this activity final private static String RECORDED_FILE = "/audio.mp4; MediaRecorder audioListener; MediaPlayer player; String pathForAppFiles; Recording (1) Make onClickListener for StartRecording

Get MediaRecorder if (audioListener!=null) audioListener.release(); if (audioListener == null) { audioListener = new MediaRecorder(); } Get path for file Note: each app runs in its own VM, with its own private directory and files. The SDK provides several tools for accessing the apps directory and files The apps directory is at /data/data/ Files are at /data/data//files FileOutputStream fos; // in fos = Context.openFileOutput(filename.txt,MODE_PRIVATE); // opens file /data/data//files\filename.txt for writing similarly FileInputStream fis; // in fis = Context.openFileInput(filename.txt); // opens file /data/data//files\filename.txt for reading MediaRecorder and MediaPlayer need the full path String pathForAppFiles = getFilesDir().getAbsolutePath(); // returns /data/data//files pathForAppFiles += RECORDED_FILE; // file name with full path

logging The SDK provides logging Log.e(tag, string) E.g., Log.e(Debug Info,Set file name); Or Log.e(file name, pathForAppFiles); The log can be seen from the DDMS Or from the command line C:\android\android-sdk-windows\platform-tools> adb d logcat C:\android\android-sdk-windows\platform-tools> adb e logcat Set up media recorder audioListener.setAudioSource(MediaRecorder.AudioSource.MIC);

audioListener.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); options DEFAULT MPEG_4: MPEG4 media file format RAW_AMR: Good for speech. Not sure if this is supported. Documentation says ToDo: change link when AMR_NB is exposed. THREE_GPP :3GPP media file format audioListener.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); Options instead of MIC : CAMCORDER Microphone audio source with same orientation as camera if available, the main device microphone otherwise DEFAULT

MIC Microphone audio source VOICE_CALL Voice call uplink + downlink audio source VOICE_DOWNLINK Voice call downlink (Rx) audio source VOICE_RECOGNITION Microphone audio source tuned for voice recognition if available, behaves like DEFAULT otherwise. VOICE_UPLINK Voice call uplink (Tx) audio source options AMR_NB : AMR (Narrowband) audio codec , for speech DEFAULT audioListener.setOutputFile(pathForAppFiles); Record try { audioListener.prepare(); audioListener.start(); } catch (Exception e) { Log.e("Audio", "Failed to prepare and start audio recording", e); } stopButton.setVisibility(View.VISIBLE); recordButton.setVisibility(View.GONE); playButton.setVisibility(View.GONE);

Stop recording Make onClickListener for stopRecordingButton if (audioListener == null) return; audioListener.stop(); audioListener.release(); audioListener = null; Log entry Log.e("Debug Info","Stopped rcording file"+pathForAppFiles); Make nice buttons stopButton.setVisibility(View.GONE); recordButton.setVisibility(View.VISIBLE); playButton.setVisibility(View.VISIBLE); Try it Run on device or emulator emulator is slow, so the quality if bad

Get file from emulator using the DDMS and play in quickTime Get file from device via adb adb -d pull /data/data/edu.udel.eleg454.AudioFun/files/audio.mp4 c:\audio.mp4 playback Get clean MediaPlayer if (player!=null) player.release(); if (player == null) { player = new MediaPlayer (); } Get file name String audioFilePath = getFilesDir().getAbsolutePath(); audioFilePath += RECORDED_FILE; Log.d("Audio filename:",audioFilePath ); Get file

Problem: the file does not have the correct permissions. See adb shell ls l There are several ways to fix this. Use the file descriptor from when the file was created. But what if we want to play a file that was not created when we run the app this time Change permissions with chmod (easiest option, but Android might not support exec() in the future!) String command = "chmod 666 " + audioFilePath.toString(); try { Runtime.getRuntime().exec(command); } catch (IOException e1) { Log.e("SetPermissions", "Couldn't set permissions", e1); } Before recording, Change permissions with java FileOutputStream fos; try { fos = openFileOutput(RECORDED_FILE, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); fos.close(); } catch (FileNotFoundException e1) {

Log.e("audio file error","could not open file"); return; } catch (IOException e) { Log.e("audio file error","could not close the file"); return; } playback try { player.setDataSource(audioFilePath); player.prepare(); player.start(); } catch (Exception e) { Log.e("Audio", "Playback failed.", e); } Nice buttons stopPlaybackButton.setVisibility(View.VISIBLE); recordButton.setVisibility(View.GONE); playButton.setVisibility(View.GONE); Stop playback stopPlayback.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (player == null)return;

player.stop(); player.release(); player = null; stopPlayback.setVisibility(View.GONE); record.setVisibility(View.VISIBLE); play.setVisibility(View.VISIBLE); } }); Also, @Override protected void onDestroy() { super.onDestroy(); if (audioListener!=null) audioListener.release(); if (player!=null) player.release(); }

Recently Viewed Presentations

  • Level G Unit 8 Vocabulary - MRS. TATUM'S AP LANGUAGE

    Level G Unit 8 Vocabulary - MRS. TATUM'S AP LANGUAGE

    Allege. Connotation:Neutral. Etymology: Anglo-French aleger, Old French eslegier "to clear at law," from Latin ex-"out of" and litigare
  • Building the Prepress-Printer-Publisher-Paper Infrastructure: Information & Technology PRIMEX

    Building the Prepress-Printer-Publisher-Paper Infrastructure: Information & Technology PRIMEX as an Example Sample Workflow Industry Standards . . . CIP4 JDF UNADI SPACE XML Ad-ID What is CIP4? What is JDF? The Job Definition Format is a comprehensive XML-based file format/proposed industry standard for end-to-end job ticket specifications...


    Peasant's Crusade. The first group to go was a disorganized rabble of peasants, led by a religious fanatic called Peter the Hermit. This is sometimes called the Peasant's Crusade. It is not one of the official Crusades with a number....
  • Value Based Purchasing Gary Swartz, JD, MPA Associate

    Value Based Purchasing Gary Swartz, JD, MPA Associate

    Value Based Purchasing . Context and history. VBP and risk adjustment (RA) defined . ... including an assessment of the professional types, practice sizes, practice geography, and patient mix that are receiving MIPS payment increases and reductions.
  • Settling the West - Auburn School District

    Settling the West - Auburn School District

    Settling the West. Know the following names/terms/places: placer mining, quartz mining, Henry . Comstock, vigilance committees, Leadville, Denver, long drive. Discuss the reason "longhorns" could survive the Great Plains.
  • Japan: Edo Culture - Laulima

    Japan: Edo Culture - Laulima

    Centralized Feudalism. Emperor's Right Hand Man. Emperor has traditionally reigned, but not ruled. Divine figure who is too busy with spiritual affairs to deal with running a kingdom. In reality, a symbiotic relationship: Emperor gets an allowance.
  • Mars Science Orbiter (2013) Presentation to the SDT

    Mars Science Orbiter (2013) Presentation to the SDT

    Goals for October 4 MEPAG Meeting Discuss Mars Program planning in light of the President's proposed budget Final Report from the joint MEPAG/SBAG Precursor Strategic Analysis Group (P-SAG) Results from the Mars Program Planning Group (MPPG) regarding options for an...
  • Expository writing Definition of Expository: "serving to ...

    Expository writing Definition of Expository: "serving to ...

    Jesse Owens was born in Alabama in 1913 and he became one of the world's greatest track and field athletes. Some galaxies have clouds of dust and gas, but others do not. Chris and Jocelyn went to the movies and...