Week 7: Lab Overview
TABLE OF CONTENTS
Lab 7: Audio (50 points)
Lab Overview
Scenario/Summary
The purpose of this lab is to add sound and the ability to take pictures to an app. Taking pictures and playing music are common things people do with mobile devices. We will use the built-in camera app to take pictures. We will also play music. Please note you will need to upload your own music or use the mp3 we have provided.
Deliverables
The deliverable for this lab is an android .apk file.
Required Software
This lab will use the following Lab Resources.
• Android Studio
Use a personal copy of the software or to access the Lab Resources, go to the Lab Resources section of the Course Resources page.
Lab Steps
Step 1: Adding media
Using Android Studio, open your app from last week.
Watch this video about adding media to Android apps.
Add two new activities named: SongActivity and TakePictureActivity.
Edit the MainMenuActivity.java and add functionality for the take pic and the song buttons so that when the user clicks those buttons, the corresponding activities will open. Add the following helper functions.
private void goSong() {
Intent intent = new Intent(MainMenuActivity.this, SongActivity.class);
this.startActivity(intent);
}
private void goTakePic() {
Intent intent = new Intent(MainMenuActivity.this, TakePictureActivity.class);
this.startActivity(intent);
}
And add the following code to the OnCreate.
Button btnTakePic=(Button) findViewById(R.id.btnTakePic);
btnTakePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goTakePic();
}
}
);
Button btnFavSong=(Button) findViewById(R.id.btnSong);
btnFavSong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goSong();
}
}
);
Edit the content_song.xml file. Add a VideoView widget in the Text view of the xml file.
You will want to force this view into a landscape orientation. Edit the AndroidManifest.xml. Locate the activity – .SongActivity and add the line in bold.
Next we need to add a song or video file. You will want to add one of your own files. It can be of type: MP3, MP4, WAV, 3GP, MIDI, FLAC, or RAW media. You can also download this sample mp3 file (Links to an external site.) if you do not have one.
Media needs to be added to the res/raw directory, which is most likely not present in your app. To add this directory right-click on app and choose New -> Android Resource Directory. Then choose RAW.
Locate your media file in Windows explorer. Right-click on your media file and choose copy. Then click on the RAW folder in Android Studio and click paste and choose OK if you receive a dialog box asking you if you want to paste it in that directory. You will see your media file in the raw directory. Please note: Your media file must be lowercase. The name of my media file is goodmorning.mp3.
Edit SongActivity.java.
Before the OnCreate method add the following code.
private VideoView vidView = null;
In the OnCreate method add the following code but change the name of the media file to the name of your media file.
vidView = (VideoView) findViewById(R.id.vidView);
vidView.setMediaController(new MediaController(this)); //add media controller
Uri video = Uri.parse(“android.resource://” +getPackageName()+ “/”+ R.raw.goodmorning);
vidView.setVideoURI(video);
vidView.setZOrderOnTop(true); //don’t merge video with other widgets
Next add the onResume and onPause methods.
protected void onResume() {
super.onResume();
vidView.start();
}
protected void onPause() {
vidView.stopPlayback();
super.onPause();
}
Save your code and run it in the emulator. When you press the Favorite Song button, you should hear your song play. Click the screen and the media navigation buttons should show. You should also note that the media player will be in landscape view.
Click image to enlarge
If you are using an emulator, skip to step 3—packaging apps for the Google Play store.
Step 2: Taking Pictures With Android (optional)
Note: This step can only be performed if you have an actual device, because taking a picture does not work with the emulator.
Edit content_take_picture.xml and add a button and an ImageView.
//code snippet
Edit the TakePictureActivity.java.
Add the following code to take a picture using the default camera app.
private static final int REQUEST_TAKE_PIC = 1;
private void takePic() {
// create an implicit intent to take a picture
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Call startActivityForResult to send the intent
if (null != getPackageManager().resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY)) {
startActivityForResult(intent, REQUEST_TAKE_PIC); //call back once work is done
}
}
When the camera app is finished, the onActivityResult code is called. This code will display the picture taken on the image view.
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent returnIntent) {
// If request succeeded, get thumbnail from the return intent
if (requestCode == REQUEST_TAKE_PIC && resultCode == RESULT_OK
&& returnIntent != null) {
Bitmap bitmap = (Bitmap) returnIntent.getExtras().get(“data”);
ImageView imageView = (ImageView) findViewById(R.id.imgFavoriteItem);
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, returnIntent);
}
Next, we need to make sure we have the proper permissions in the manifest file. Edit the AndroidManifest.xml and add the following code before
Run your code on your Android device. Click on the Take Picture button. When it opens, click Take Picture again. Then your camera app will open. Take a picture. You will see an OK and a RETRY button. Choose whichever and Take Pic until you are satisfied with the picture you have taken. Then click OK. You will see the picture show up on your screen. This is a screenshot of the picture I took with my phone of a stuffed white tiger toy.
Click image to enlarge
Step 3: Packaging Apps for the Google Play Store
Android apps must be distributed with a signed certificate. This is required before they can be deployed. You must sign an app with your own release key for the Google Play store. Updates to an app must be signed with the same key. In this step, we will generate a signing key and then package the app in an .apk file.
With your project open, select Build->Generate Signed APK. Your signing key is encrypted and kept in a keystore file. You will create a new keystore. Click Create New.
Choose a directory and a filename for the key store path.
You will be asked for two different passwords. One for the keystore file and one for accessing your signing key. You will also provide the details for your certificate.
On the next screen, you can decide where to save your apk file and then click finish. Your apk file is ready to go and is distributable!
Step 4: Finalize the File
Finalize and submit your apk file.
Don’t forget to submit your lab.







* You can also use 2CO option if you want to purchase through Credit Cards/Paypal but make sure you put the correct billing information otherwise you wont be able to receive any download link.
* Your paypal has to be pre-loaded in order to complete the purchase or otherwise please discuss it with us at [email protected].
* As soon as the payment is received, download link of the solution will automatically be sent to the address used in selected payment method.
* Please check your junk mails as the download link email might go there and please be patient for the download link email. Sometimes, due to server congestion, you may receive download link with a delay.
* All the contents are compressed in one zip folder.
* In case if you get stuck at any point during the payment process, please immediately contact us at [email protected] and we will fix it with you.
* We try our best to reach back to you on immediate basis. However, please wait for atleast 8 hours for a response from our side. Afterall, we are humans.
* Comments/Feedbacks are truely welcomed and there might be some incentives for you for the next lab/quiz/assignment.
* In case of any query, please donot hesitate to contact us at [email protected].
* MOST IMPORTANT Please use the tutorials as a guide and they need NOT to be used for any submission. Just take help from the material.
******************************************** Good Luck ***************************************************
Any personal information received will only be used to fill your order. We will not sell or redistribute your information to anyone.
We will try our best to resolve the issue and if still persists we can discuss for a refund in case its required.