chapter 16 voice recorder app
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Chapter 16Voice Recorder AppPresented by: Jon Alsup, Mason Alsup, and Paul McBroom
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.
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.
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
Demo Slide
Run it, dood!
GUI design
Visualization area
Record Button
The disabled Save andDelete buttons areenabled only when there isa new recording
Touch to view saved recordings
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
ToggleButtontoggles betweenPlay and Pause
Touch the X to delete recording
Touch the envelopeTo e-mail recording
Touch the nameof a clip to play it
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
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
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
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
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
MediaRecorder
Import statement that lets us use MediaRecorder
Declaring a MediaRecorder
Clean up the app when it goes to the background or is otherwise paused.
Methods of MediaRecorder
A couple more methods of MediaRecorder (hint hint)
VisualizerView Class
Import Statements of VisualizerView Class
Methods for VisualizerView
Declare a VisualizerView in VoiceRecorder.java
Referencing
While recording, update theVisualizerView constantly
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.
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.
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.
Nothing fancy here. I just included this to show why the list ofsaved files is always in alphabetical order.
Just using the array from before to usethe file name inthe TextView.
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.
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.
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.
Here, we set what file we are to play from the filepath we gotfrom the previous slide.
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?
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.
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.
Wrap-up
• Know the Methods in MediaRecorder
• VisualizerView is a subclass of View
• File Class allows us to save, delete, rename files
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.