Recording and playing audio App Make app Audio

  • Slides: 10
Download presentation
Recording and playing audio

Recording and playing audio

App • Make app Audio. Fun with 4 buttons – – Start recording (id=Start.

App • Make app Audio. Fun with 4 buttons – – Start recording (id=Start. Recording) Stop recording (id=Stop. Recording) Start playback (id=Start. Playback) Stop playback (id=Stop. Playback) • 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. mp 4“; Media. Recorder audio. Listener; Media. Player player; String path. For. App. Files;

Recording (1) • Make on. Click. Listener for Start. Recording • • • Get

Recording (1) • Make on. Click. Listener for Start. Recording • • • Get Media. Recorder – if (audio. Listener!=null) audio. Listener. release(); – if (audio. Listener == null) { audio. Listener = new Media. Recorder(); } 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/<package name> – Files are at /data/<package name>/files • File. Output. Stream fos; // in java. io. File. Output. Stream • fos = Context. open. File. Output(“filename. txt”, MODE_PRIVATE); // opens file /data/<package name>/filesfilename. txt for writing – similarly • File. Input. Stream fis; // in java. io. File. Output. Stream • fis = Context. open. File. Input(“filename. txt”); // opens file /data/<package name>/filesfilename. txt for reading Media. Recorder and Media. Player need the full path – String path. For. App. Files = get. Files. Dir(). get. Absolute. Path(); // returns /data/<package name>/files – path. For. App. Files += RECORDED_FILE; // file name with full path

logging • The SDK provides logging – – – Log. e(tag, string) E. g.

logging • The SDK provides logging – – – Log. e(tag, string) E. g. , Log. e(“Debug Info”, ”Set file name”); Or Log. e(“file name”, path. For. App. Files); The log can be seen from the DDMS Or from the command line • C: android-sdk-windowsplatform-tools> adb –d logcat • C: android-sdk-windowsplatform-tools> adb –e logcat

Set up media recorder • audio. Listener. set. Audio. Source(Media. Recorder. Audio. Source. MIC);

Set up media recorder • audio. Listener. set. Audio. Source(Media. Recorder. Audio. Source. MIC); – • audio. Listener. set. Output. Format(Media. Recorder. Output. Format. DEFAULT); – • options • DEFAULT • MPEG_4: MPEG 4 media file format • RAW_AMR: Good for speech. Not sure if this is supported. Documentation says “To. Do: change link when AMR_NB is exposed. “ • THREE_GPP : 3 GPP media file format audio. Listener. set. Audio. Encoder(Media. Recorder. Audio. Encoder. 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 audio. Listener. set. Output. File(path. For. App. Files);

Record try { audio. Listener. prepare(); audio. Listener. start(); } catch (Exception e) {

Record try { audio. Listener. prepare(); audio. Listener. start(); } catch (Exception e) { Log. e("Audio", "Failed to prepare and start audio recording", e); } stop. Button. set. Visibility(View. VISIBLE); record. Button. set. Visibility(View. GONE); play. Button. set. Visibility(View. GONE);

Stop recording • Make on. Click. Listener for stop. Recording. Button if (audio. Listener

Stop recording • Make on. Click. Listener for stop. Recording. Button if (audio. Listener == null) return; audio. Listener. stop(); audio. Listener. release(); audio. Listener = null; • Log entry Log. e("Debug Info", "Stopped rcording file"+path. For. App. Files); • Make nice buttons stop. Button. set. Visibility(View. GONE); record. Button. set. Visibility(View. VISIBLE); play. Button. set. Visibility(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 quick. Time • Get file from device via adb • adb -d pull /data/edu. udel. eleg 454. Audio. Fun/files/audio. mp 4 c: audio. mp 4

playback • Get clean Media. Player if (player!=null) player. release(); if (player == null)

playback • Get clean Media. Player if (player!=null) player. release(); if (player == null) { player = new Media. Player (); } • Get file name String audio. File. Path = get. Files. Dir(). get. Absolute. Path(); audio. File. Path += RECORDED_FILE; Log. d("Audio filename: ", audio. File. Path ); • 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 " + audio. File. Path. to. String(); try { Runtime. get. Runtime(). exec(command); } catch (IOException e 1) { Log. e("Set. Permissions", "Couldn't set permissions", e 1); } • Before recording, Change permissions with java File. Output. Stream fos; try { fos = open. File. Output(RECORDED_FILE, Context. MODE_WORLD_READABLE|Context. MODE_WORLD_WRITEABLE); fos. close(); } catch (File. Not. Found. Exception e 1) { 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. set. Data. Source(audio. File. Path); player. prepare(); player. start(); }

playback try { player. set. Data. Source(audio. File. Path); player. prepare(); player. start(); } catch (Exception e) { Log. e("Audio", "Playback failed. ", e); } Nice buttons stop. Playback. Button. set. Visibility(View. VISIBLE); record. Button. set. Visibility(View. GONE); play. Button. set. Visibility(View. GONE);

Stop playback stop. Playback. set. On. Click. Listener(new View. On. Click. Listener() { public

Stop playback stop. Playback. set. On. Click. Listener(new View. On. Click. Listener() { public void on. Click(View v) { if (player == null)return; player. stop(); player. release(); player = null; stop. Playback. set. Visibility(View. GONE); record. set. Visibility(View. VISIBLE); play. set. Visibility(View. VISIBLE); } }); Also, @Override protected void on. Destroy() { super. on. Destroy(); if (audio. Listener!=null) audio. Listener. release(); if (player!=null) player. release(); }