0% found this document useful (0 votes)
13 views2 pages

Multimedia Notes

The document discusses how to work with audio, video, and camera in Android applications. It provides examples of playing audio and video files from the raw resource directory or external storage. It also demonstrates how to launch the camera app to capture an image or video and return the result to the calling activity.

Uploaded by

Nitin Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Multimedia Notes

The document discusses how to work with audio, video, and camera in Android applications. It provides examples of playing audio and video files from the raw resource directory or external storage. It also demonstrates how to launch the camera app to capture an image or video and return the result to the calling activity.

Uploaded by

Nitin Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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 layout.xml file and insert a Button control
 Open you main activity (.java file), and place the following lines of code inside

Button b = (Button) findViewById(R.id.button1);


b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
MediaPlayer p = MediaPlayer.create(Main.this, R.raw.abc.mp3);
p.start();
}
});
* 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(R.id.videoView1);


v.setVideoPath("/sdcard/abc.mp4");
v.start();
// include the following line if you want your video view control to have play, pause, FF controls
v.setMediaController(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 layout.xml 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(R.id.imageView1);
Button b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Bitmap bmap = (Bitmap) data.getExtras().get("data");


iv.setImageBitmap(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 AndroidManifest.xml file and add a new User Permission and select
android.permission.CAMERA from the drop down

You might also like