1 / 36

Chapter 16 Voice Recorder App

Chapter 16 Voice Recorder App. Presented by: Jon Alsup, Mason Alsup, and Paul McBroom. Objectives In this chapter, you’ll: ■ Specify permissions for recording audio and writing to a device’s external storage. ■ Record audio files using a MediaRecorder.

sema
Download Presentation

Chapter 16 Voice Recorder App

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 16Voice Recorder App Presented by: Jon Alsup, Mason Alsup, and Paul McBroom

  2. Objectives In this chapter, you’ll: ■ Specify permissions for recording audio and writing to a device’s external storage. ■ Record audio files using a MediaRecorder. ■ Create a visual representation of the user’s audio input. ■ Use an ArrayAdapter to display the names of a directory’s files in ListView.

  3. Objectives ■ Use the File class to save files in a device’s external storage directory. ■ Use the File class to delete files from a device’s external storage directory. ■ Allow the user to send a recording as an e-mail attachment.

  4. Voice Recorder App • Allows users to record sounds using the device’s microphone • Allows recorded sounds to be saved and played back later • Lets users send saved recordings as email attachments • Has an option to view saved recordings • Allows user to delete and rename a saved recording

  5. Demo Slide Run it, dood!

  6. GUI design Visualization area  The disabled Save and Delete buttons are enabled only when there is a new recording Record Button   Touch to view saved recordings

  7. Visualization of the user’s recording When the user chooses to  save a recording, the user will be prompted to name it Save and Delete Buttons are now enabled because the user pressed the Stop ToggleButton (which now displays Record) to stop recording

  8. ToggleButton toggles between Play and Pause Touch the X to delete recording Touch the envelope To e-mail recording Touch the name of a clip to play it

  9. Technologies Overview • AndroidManifest.xml file • To allow an app access to shared Android services, need <uses-permission> element • Allows microphone use and writing data to external storage (to save a recording) • ListView • Want the ListView items to be clickable • Clicking on listview items will play the corresponding recording

  10. Technologies Overview • ImageView • Want ImageViews to also be clickable • Clickable ImageViews used for the “delete” and “e-mail” button • Emailing a Recording as an attachment • Use an Intent and an Activity chooser to allow the user to send a recording in an email • Works on any device that supports this capability

  11. Technologies Overview • ToggleButton • Change the Icons by using a State List Drawable • Define a state list drawable with a root <selector> element with <item> elements for each state • Set the desired state as the ToggleButton's android:drawableTop attribute

  12. Technologies Overview • MediaRecorder • Voice Recorder app uses a MediaRecorder (package android.media) to record the user's voice • Records using the device's microphone and saves it to an audio file on the device

  13. Technologies Overview • File Class • Saves recording in a temp file • Can permanently save temp file if user chooses to which will also allow renaming the file • Will also delete chosen saved recordings

  14. MediaRecorder Import statement that lets us use MediaRecorder

  15. Declaring a MediaRecorder Clean up the app when it goes to the background or is otherwise paused.

  16. Methods of MediaRecorder A couple more methods of MediaRecorder (hint hint)

  17. VisualizerView Class Import Statements of VisualizerView Class

  18. Methods for VisualizerView

  19. Declare a VisualizerView in VoiceRecorder.java

  20. Referencing

  21. While recording, update the VisualizerView constantly

  22. File Class To use the file class, we must import java.io.File Java.util.collections is highlighted because it will be used to sort a list of files. Although we will not be talking about it, we note that there is a new concept of a compound button that is used in this program to change the record button to a stop button and back to a record button.

  23. The class SavedRecordings in this program is how the program interacts with the saved files • Note that the nowPlayingTextView is a TextView. This will both display the name of the file and let us remember it's name without using an array.

  24. getExternalFilesDir(null) is used to return the absolute path to the directory on an external file system. The method getExternalFilesDir(null).list() returns a list of the filenames.

  25. Nothing fancy here. I just included this to show why the list of saved files is always in alphabetical order.

  26. Just using the array from before to use the file name in the TextView.

  27. This was interesting. In this method we choose to email the file. The first command again uses the getExternalFilesDir(null) to fetch the path to the absolute path along with the filename. Hence, for some file named someFile, the value in data would Be File://path_to_external_directory/someFile. The second command is an intent that opens a stream that will supply the data of that file to send.

  28. When a user wants to delete a file from the external file directory, we simply get the path (note that File.seperator is just a “/”, but since not all operating systems use this as a standard, using this constant helps with the applications portability) and call The file.delete function. We also remove it from the savedRecordingsAdapter.

  29. Here, we are, again, getting the filepath by first getting the textView that was clicked, and then...well, I hope you know how this works by now! We note that this is to change what is being played in the List of files screen.

  30. Here, we set what file we are to play from the filepath we got from the previous slide.

  31. In VoiceRecorder.java, we again import Java.io.File In case of a pause during a recording, we obviously don't want to keep an incomplete recording, so we delete the temporary file. What temporary file, you ask?

  32. The temporary file we use to record a file. • The process of recording: • In the RecordbuttonListener, We first start by creating a temporary file. • We then set the output file of the recorder to the temporary files absolute path. • After this, we let the recorder do it's thing until we press the stop button that appears where the record button was.

  33. if (name.length() != 0) { // create Files for temp file and new file name File tempFile = (File) v.getTag(); File newFile = new File( getExternalFilesDir(null).getAbsolutePath() + File.separator + name + ".3gp"); tempFile.renameTo(newFile); // rename the file saveButton.setEnabled(false); // disable deleteButton.setEnabled(false); // disable recordButton.setEnabled(true); // enable viewSavedRecordingsButton.setEnabled(true); // enable } // end if else { // display message that slideshow must have a name Since we set the savebuttons tag to the path of the temp file, then we bring the file into the onclick() function of the saveButtonListener. We then create a new file with the user-defined name and rename the temp file to that name. I will not go into the deleteButtonListener, since we have already shown how to delete files and it is the same idea.

  34. Wrap-up • Know the Methods in MediaRecorder • VisualizerView is a subclass of View • File Class allows us to save, delete, rename files

  35. 3 Questions to know 1. The ___________ class allows users to save files. 2. _______________ records audio using the device's microphone. 3. Name two methods of MediaRecorder.

More Related