Recording and playing audio - PowerPoint PPT Presentation

recording and playing audio n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Recording and playing audio PowerPoint Presentation
Download Presentation
Recording and playing audio

play fullscreen
1 / 10
Recording and playing audio
69 Views
Download Presentation
dora
Download Presentation

Recording and playing audio

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Recording and playing audio

  2. App • Make app AudioFunwith 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“; • MediaRecorderaudioListener; • MediaPlayerplayer; • String pathForAppFiles;

  3. 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/<package name> • Files are at /data/data/<package name>/files • FileOutputStreamfos; // in java.io.FileOutputStream • fos = Context.openFileOutput(“filename.txt”,MODE_PRIVATE); // opens file /data/data/<package name>/files\filename.txt for writing • similarly • FileInputStreamfis; // in java.io.FileOutputStream • fis = Context.openFileInput(“filename.txt”); // opens file /data/data/<package name>/files\filename.txt for reading • MediaRecorder and MediaPlayer need the full path • String pathForAppFiles = getFilesDir().getAbsolutePath(); // returns /data/data/<package name>/files • pathForAppFiles += RECORDED_FILE; // file name with full path

  4. 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

  5. Set up media recorder • audioListener.setAudioSource(MediaRecorder.AudioSource.MIC); • 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 • 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 • AMR_NB : AMR (Narrowband) audio codec , for speech • DEFAULT • audioListener.setOutputFile(pathForAppFiles);

  6. 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);

  7. Stop recording if (audioListener == null) return; audioListener.stop(); audioListener.release(); audioListener= null; • Make onClickListener for stopRecordingButton • Log entry Log.e("Debug Info","Stoppedrcording 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

  8. playback if (player!=null) player.release(); if (player == null) { player = new MediaPlayer (); } • Get clean 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); } • Change permissions with java FileOutputStreamfos; 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; } Before recording,

  9. 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);

  10. 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); } }); @Override protected void onDestroy() { super.onDestroy(); if (audioListener!=null) audioListener.release(); if (player!=null) player.release(); } Also,