Podcast
Questions and Answers
What is the correct way to refer to an audio file in Java code?
What is the correct way to refer to an audio file in Java code?
What is the primary purpose of the MediaPlayer class in Android?
What is the primary purpose of the MediaPlayer class in Android?
What happens when an activity A launches another activity B in response to an event?
What happens when an activity A launches another activity B in response to an event?
What is the purpose of the seekTo method in the MediaPlayer class?
What is the purpose of the seekTo method in the MediaPlayer class?
Signup and view all the answers
What is the correct format for sound filenames in Android?
What is the correct format for sound filenames in Android?
Signup and view all the answers
What happens when an activity is finished in Android?
What happens when an activity is finished in Android?
Signup and view all the answers
What is the purpose of passing a unique ID number to startActivityForResult?
What is the purpose of passing a unique ID number to startActivityForResult?
Signup and view all the answers
What happens when startActivityForResult is called?
What happens when startActivityForResult is called?
Signup and view all the answers
What is the purpose of the onActivityResult method?
What is the purpose of the onActivityResult method?
Signup and view all the answers
How does an activity send a result back to the calling activity?
How does an activity send a result back to the calling activity?
Signup and view all the answers
What is the purpose of the Intent in the onActivityResult method?
What is the purpose of the Intent in the onActivityResult method?
Signup and view all the answers
What happens when the setResult and finish methods are called?
What happens when the setResult and finish methods are called?
Signup and view all the answers
What is the purpose of an intent in Android?
What is the purpose of an intent in Android?
Signup and view all the answers
What happens when you create a new activity in Android Studio?
What happens when you create a new activity in Android Studio?
Signup and view all the answers
What is the purpose of AndroidManifest.xml?
What is the purpose of AndroidManifest.xml?
Signup and view all the answers
How do you extract extra data from an intent in the second activity?
How do you extract extra data from an intent in the second activity?
Signup and view all the answers
What is the purpose of the startActivity method?
What is the purpose of the startActivity method?
Signup and view all the answers
What is the difference between an intent and an activity?
What is the difference between an intent and an activity?
Signup and view all the answers
What is the purpose of putExtra method?
What is the purpose of putExtra method?
Signup and view all the answers
What happens if you don't add an activity to the AndroidManifest.xml?
What happens if you don't add an activity to the AndroidManifest.xml?
Signup and view all the answers
Study Notes
Playing Sound Effects
- Place sound files (e.g.,
.WAV
,.MP3
) in the project folderapp/src/main/res/raw
. - In Java code, refer to the audio file as
R.raw.filename
(without the extension). - Use simple file names with only letters and numbers.
Loading and Playing Sound Clips
- Use Android's
MediaPlayer
class to load and play sound clips. - Methods available:
-
create()
: creates aMediaPlayer
object. -
start()
: starts playing the sound clip. - Others:
stop()
,pause()
,isLooping()
,isPlaying()
,getCurrentPosition()
,release()
,seekTo()
,setDataSource()
,setLooping()
.
-
Multiple Activities
- Many apps have multiple activities, e.g., an address book app with a list of contacts and a separate activity for viewing details.
- One activity can launch another activity in response to an event and pass data to it.
- The second activity can send data back to the first activity when it's done.
Adding an Activity
- In Android Studio, right-click "app" and select "New" > "Activity" to create a new activity.
- This creates a new
.xml
file inres/layouts
and a new.java
class insrc/java
, and adds information toAndroidManifest.xml
about the activity.
Activities in Manifest
- Every activity has an entry in
AndroidManifest.xml
, added automatically by Android Studio.
Intents
- An
Intent
is a bridge between activities, allowing one activity to invoke another. - Intents can store extra data as "parameters" to pass to the second activity.
- The second activity can return information back to the caller if needed.
Creating an Intent
- Create an
Intent
object and callstartActivity()
with it to launch another activity. - Use
putExtra()
to pass parameters to the second activity.
Extracting Extra Data
- In the second activity, use
getIntent()
to access theIntent
that spawned it. - Use methods like
getExtra()
,getIntExtra()
,getStringExtra()
, etc. to extract data stored in the intent.
Waiting for a Result
- Use
startActivityForResult()
instead ofstartActivity()
to wait for a result from the called activity. - Pass a unique ID number to represent the action being performed.
- Write an
onActivityResult()
method to handle the result.
Sending Back a Result
- In the second activity, create an
Intent
to send data back. - Use
setResult()
andfinish()
to end the called activity.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Learn how to play sound effects in an Android app, including loading and managing audio files using the MediaPlayer class. Understand how to reference sound files in Java code.