Multithreading in Android
Multithreading in Android
Statement Purpose:
Today we will create a simple Timer timer app. We will learn the use of handler through this stopwatch app.
Activity Outcomes:
After completing this chapter we will be able to understand the following topics.
- Multithreading
- Multimedia in Android
- Media Player API
- Camera API
- Running the Multithreading App
Instructor Note:
Multithreading
Multi-threading is defined as a feature through which we can run two or more concurrent threads of a process. In this a process, the common data is shared among all these threads also known as sub-processes exclusively.
Multimedia in Android
Multimedia capabilities, or the playing and recording of audio and video, is one such significant task that many users find to be of great value. Take a quick look around us, we will find people using the phone as a means to enjoy a variety of programs as well as share self-recorded media among friends. Android provides APIs to easily access this capability as well as embed multimedia and its manipulation directly within an application. Usually multimedia involves in performing the following basic functions.
- Playing audio and video
- Controlling the camera
- Recording audio
- Recording video
We will discuss the ‗Stagefright‘ architecture from Google. Because the foundation of Android‘s multimedia platform is Google‘s new media platform Stagefright. Stagefright, as of now, supports the following core media files, services, and features:
- Interfaces for third-party and hardware media codecs, input and output devices, and content policies.
- Media playback, streaming, downloading, and progressive playback, including third- Generation Partnership Program (3GPP), Moving Picture Experts Group 4 (MPEG-4), Advanced Audio Coding (AAC), and Moving Picture Experts Group (MPEG) Audio Layer 3 (MP3)
- Network protocols including RTSP (TRP, SDP), HTTP progressive streaming, and HTTP live streaming.
- Video and image encoders and decoders, including MPEG-4, International Tele- communication Union H.263 video standard (H.263), Advanced Video Coding (AVC H.264), and the Joint Photographic Experts Group (JPEG).
- Speech codecs, including Adaptive Multi-Rate audio codecs AMR-NB and AMR-WB.
- Audio codecs, including MP3, AAC, and AAC+
- Media recording, including 3GPP, VP8, MPEG-4, and JPEG.
- Video telephony based on the 3GPP video conferencing standard 324-M.
Media Player API
Android Multimedia Framework – android.media API
The android multimedia framework provides developers a way to easily integrate audio and video playback into applications, and supports most of the common media types. The Media Player class is the key in android multimedia framework. It can be used to play media on the local file system, media files stored in the application‘s resources, as well as data streaming over a network connection.
Playing Audio
Probably the most basic need for multimedia on a cell phone is the ability to play audio files, whether new ringtones, MP3s, or quick audio notes. Media- Player of android is easy to use. To play an MP3 file follow these steps:
- Place the MP3 in the res/raw directory in a project (note that we can also use a URI to access files on the network or via the internet).
- Create a new instance of the MediaPlayer, and reference the MP3 by calling MediaPlayer.create().
- Call the MediaPlayer methods prepare() and start().
Playing Video
Playing a video is slightly more complicated than playing audio with the MediaPlayer API, because we have to provide a view surface for our video to play on. Android has a VideoView widget that handles this task for us. This widget can be used with any layout manager.
Android also provides a number of display options, including scaling and tinting. cseitquestions.blogspot.in
Android Camera API
Usually every android device has at least one camera (back camera) or many devices have multiple cameras like front camera and rear camera. There is a default application available in each android device to access this camera. We can also access this application into our application as intent. Alternatively, we can also have our own camera application using android camera API. There is set of methods and class that support camera in android device.
Camera API in Detail:
We create intent to access camera in android. This intent can created using default camera application or using android camera API. we will create an application using camera API and will create intent to access this. This application will capture an image and will save it to the image directory into the android device. Once an image has been captured, this application will preview this image, while saving. Let us create a new java class that will capture an image as listed.
Running the Multithreading App
Step 1: Select File -> New -> Project -> Android Application Project (or) AndroidProject. Fill the forms and click ―Finish‖ button.
Step 2: Open res -> layout -> activity_main.xml -> click Design -> Put the necessarycomponents in the layout.
Step 3: Create a layout file for UI ( Design for UI after that the code will be generatedautomatically in activity_main.xml )
Introduction
Today we will create a simple stopwatch timer We will learn the use of handler through this stopwatch app. Following will be the functionality of our stopwatch timer app:
- Start-pause the stopwatch timer app.
- Reset the stopwatch timer app.
- Color change while starting or pausing time
Activities:
Activity 1:
Steps for making App
- Project Setup for Timer App Tutorial:
- Go to file–>new–>Project
Select Android–>Android Application Project
Xml Files for Timer App Tutorial
- Go to res–>values–>strings.xml and add the following code into
- <?xml version=“1.0” encoding=“utf-8”?>
- <resources>
- <string name=“app_name”>StopwatchTutorial</string>
- <string name=“action_settings”>Settings</string>
- <string name=“hello_world”>Hello world!</string>
- <string name=“start_time”>00:00:00</string>
- <string name=“start”>Start</string>
- <string name=“reset”>Reset</string>
- <string name=“title”>Androidplus Timer</string>
- </resources>
package androidplus.org. Timertutorial;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button butnstart, butnreset;
TextView time;
long starttime = 0L;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedtime = 0L;
int t = 1;
int secs = 0;
int mins = 0;
int milliseconds = 0;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butnstart = (Button) findViewById(R.id.start);
butnreset = (Button) findViewById(R.id.reset);
time = (TextView) findViewById(R.id.timer);
butnstart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (t == 1) {
butnstart.setText(“Pause”);
starttime = SystemClock.uptimeMillis();
handler.postDelayed(updateTimer, 0);
t = 0;
} else { butnstart.setText(“Start”);
time.setTextColor(Color.BLUE);
timeSwapBuff += timeInMilliseconds;
handler.removeCallbacks(updateTimer);
t = 1;
}}
});
OUTPUT
Home Activities:
You have to add a time delay to the taking of the picture. Make SnapShot example so that picture is taken ten seconds after pushing a button.