Download - Chapter 16 Voice Recorder App
![Page 1: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/1.jpg)
Chapter 16Voice Recorder AppPresented by: Jon Alsup, Mason Alsup, and Paul McBroom
![Page 2: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/2.jpg)
ObjectivesIn 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.
![Page 3: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/3.jpg)
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.
![Page 4: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/5.jpg)
Demo Slide
Run it, dood!
![Page 6: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/6.jpg)
GUI design
Visualization area
Record Button
The disabled Save andDelete buttons areenabled only when there isa new recording
Touch to view saved recordings
![Page 7: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/7.jpg)
Visualization of theuser’s recording
Save and Delete Buttonsare now enabled becausethe user pressed the StopToggleButton (whichnow displays Record) tostop recording
When the user chooses to save a recording, the user will be prompted to name it
![Page 8: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/8.jpg)
ToggleButtontoggles betweenPlay and Pause
Touch the X to delete recording
Touch the envelopeTo e-mail recording
Touch the nameof a clip to play it
![Page 9: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/9.jpg)
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
![Page 10: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/10.jpg)
Technologies OverviewImageView
Want ImageViews to also be clickableClickable ImageViews used for the “delete”
and “e-mail” buttonEmailing a Recording as an attachment
Use an Intent and an Activity chooser to allow the user to send a recording in an emailWorks on any device that supports this
capability
![Page 11: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/11.jpg)
Technologies OverviewToggleButton
Change the Icons by using a State List DrawableDefine 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
![Page 12: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/12.jpg)
Technologies OverviewMediaRecorder
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
![Page 13: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/13.jpg)
Technologies OverviewFile 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
![Page 14: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/14.jpg)
MediaRecorder
Import statement that lets us use MediaRecorder
![Page 15: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/15.jpg)
Declaring a MediaRecorder
Clean up the app when it goes to the background or is otherwise paused.
![Page 16: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/16.jpg)
![Page 17: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/17.jpg)
Methods of MediaRecorder
A couple more methods of MediaRecorder (hint hint)
![Page 18: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/18.jpg)
VisualizerView Class
Import Statements of VisualizerView Class
![Page 19: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/19.jpg)
Methods for VisualizerView
![Page 20: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/20.jpg)
Declare a VisualizerView in VoiceRecorder.java
![Page 21: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/21.jpg)
Referencing
![Page 22: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/22.jpg)
While recording, update theVisualizerView constantly
![Page 23: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/23.jpg)
File ClassTo 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.
![Page 24: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/24.jpg)
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.
![Page 25: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/25.jpg)
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.
![Page 26: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/26.jpg)
Nothing fancy here. I just included this to show why the list ofsaved files is always in alphabetical order.
![Page 27: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/27.jpg)
Just using the array from before to usethe file name inthe TextView.
![Page 28: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/28.jpg)
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 wouldBe File://path_to_external_directory/someFile.
The second command is an intent that opens a stream that willsupply the data of that file to send.
![Page 29: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/29.jpg)
When a user wants to delete a file from the external file directory, we simply get thepath (note that File.seperator is just a “/”, but since not all operating systems usethis as a standard, using this constant helps with the applications portability) and callThe file.delete function. We also remove it from the savedRecordingsAdapter.
![Page 30: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/30.jpg)
Here, we are, again, getting the filepath by first getting the textView that was clicked, and then...well, I hope you know how this worksby now! We note that this is to change what is being played in theList of files screen.
![Page 31: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/31.jpg)
Here, we set what file we are to play from the filepath we gotfrom the previous slide.
![Page 32: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/32.jpg)
In VoiceRecorder.java, we again importJava.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?
![Page 33: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/33.jpg)
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 filesabsolute path.After this, we let the recorder do it's thing until we press the stop buttonthat appears where the record button was.
![Page 34: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/34.jpg)
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 tempfile to that name.
I will not go into the deleteButtonListener, since we have already shownhow to delete files and it is the same idea.
![Page 35: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/35.jpg)
Wrap-up
• Know the Methods in MediaRecorder
• VisualizerView is a subclass of View
• File Class allows us to save, delete, rename files
![Page 36: Chapter 16 Voice Recorder App](https://reader035.vdocuments.net/reader035/viewer/2022062305/56816348550346895dd3d71a/html5/thumbnails/36.jpg)
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.