How to work with Audio/Video files and access Camera in android
For audio/video files, you need to create a new folder/directory called ‘raw’ inside ‘res’
directory and place your files inside the ‘raw’ directory
Or you can access files from your SD card or phone memory for playing, if you don't want to
include them in your application
Using Audio Example
Let’s suppose we want to play a small beep sound whenever a button is
pressed/clicked/touched in our App
Create a blank project
First you need to create a new directory named ‘raw’ inside ‘res’ directory
(yourprojectname/res/raw)
Now place a sound clip inside this newly created directory
Open you [Link] file and insert a Button control
Open you main activity (.java file), and place the following lines of code inside
Button b = (Button) findViewById([Link].button1);
[Link](new OnClickListener() {
@Override
public void onClick(View arg0) {
MediaPlayer p = [Link]([Link], [Link].mp3);
[Link]();
}
});
* abc.mp3 is the name of the audio clip
Using Video Example
If we want to play a video file saved in our SD card or phone memory, we need a video player
control
Create a blank project
Place a Video View control on your main layout
Edit main activity (.java file) to include the following lines of code
VideoView v = (VideoView) findViewById([Link].videoView1);
[Link]("/sdcard/abc.mp4");
[Link]();
// include the following line if you want your video view control to have play, pause, FF controls
[Link](new MediaController(this));
* abc.mp4 is the name of the video file present in the SD
Using Camera Example
For access or use camera, you need to access/communicate directly with the built-in camera
App
Your App will communicate with camera App and get image/video captured/recorded and then
you can use it
Create a blank project, in this example we’ll capture an image and then display it on our main
activity
Edit the [Link] to insert a Button and an Image View control
Edit the main activity (.java file) and place the following lines of code in it
iv = (ImageView) findViewById([Link].imageView1);
Button b = (Button) findViewById([Link].button1);
[Link](new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent([Link].ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);
Bitmap bmap = (Bitmap) [Link]().get("data");
[Link](bmap);
}
* An intent is needed to communicate with the camera App. The results are expected (image/video
file), so the start activity method is called for results. The bitmap image returned is then displayed
using Image View control
Finally you need to provide permission to your App so that it can use camera of the phone
Open you [Link] file and add a new User Permission and select
[Link] from the drop down