Download Course Module

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Multimedia
Android provides means of accessing its hardware through specific APIs such as a multimedia
playback library, and specific sensors library for monitoring phone’s orientation for example.
This module will describe how you can play multimedia files such as mp3s, and how you can
display images.
MP3 Playback
Note: The code samples from this section (MP3 Playback section only) are taken from “Hello
Android” book by Ed Burnette (http://www.pragprog.com/titles/eband/hello-android).
This section is concerned with multimedia playback in Android in general, and playing mp3s in
the Sudoku example in particular. The playback functionality is handled by the MediaPlayer
class, and you will play back media stored as application resources (in the /res/raw/ folder
inside the Sudoku application).
To play the media, there are several ways you can go about it. In the Sudoku example, you will
first create a MediaPlayer instance by using the static method create of the MediaPlayer
class, which takes as inputs the Context to use, and the raw resource id for the resource to use as
a datasource (the actual mp3 file in our case). If the creation fails, null is returned. After the
method returns successfully, the MediaPlayer object is in the Prepared state. To start the
playback, the start method has to be called on the MediaPlayer instance. After the method
return successfully, the MediaPlayer is in the Started state. Below is the part of code used in
our Sudoku application to start the media playback:
private static MediaPlayer mp = null;
/** Stop old song and start new one */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences
if (Settings.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
This method is being called either from the Sudoku or Game class. The context is usually the
activity from where the method is being called. The resource parameter will look like
R.raw.main or R.raw.game, where the resources link to main.mp3 and game.mp3 found in
the raw directory as mentioned above. Another method used is setLooping that sets the player
to be looping if the parameter is set to true. The media is played only if the option is not
disabled in the application preferences menu. The stop method will be described shortly.
1
To stop media playback, there are several methods that need to be called, in a specific order. The
first method is stop, called on the MediaPlayer instance, changing its state to Stopped. Since
you are now done using the MediaPlayer, it is considered a good practice to release all
resources that were associated with this player. In order to achieve this, you call the release
method, which will take the MediaPlayer object into the End state. Below is the part of code
used in our Sudoku application to stop the media playback:
/** Stop the music */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
This method is also being called either from the Sudoku or Game class. The context is usually
the activity from where the method is being called. Setting the mp reference to null helps the
Garbage Collector free up memory for the MediaPlayer. Although it is not necessary, it is
considered a good practice, especially in mobile development.
Beside playing multimedia, you can also record audio or video, and access the Camera. To learn
more about this, please check any of the references mentioned in the References section of this
document.
Images
In this section we are mainly concerned with displaying (still) images on Android’s screen. The
main class used is ImageView (similar to TextView but instead of displaying text, it displays
images). Each widget takes an android:src attribute (in an XML layout) to specify
what picture to use and usually reference a drawable resource (image file found inside the
drawable
directory
of
your
project).
Another
important
property
is
android:adjustViewBounds that you want to set to true if you want the ImageView to adjust
its bounds to preserve the aspect ratio of its drawable. More details on the available properties
can be found at http://developer.android.com/reference/android/widget/ImageView.html.
As an example, we will create an ImageDisplayer Android Project in Eclipse (the code and
resources are already available). The main Activity is shown below:
package edu.csi.android.images;
import android.app.Activity;
import android.os.Bundle;
public class ImageDisplayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_display);
}
}
2
The image_display layout is found in the image_display.xml file located in the layout directory
of your project:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:text="@string/image_name" />
<ImageView
android:src="@drawable/image_default"
android:adjustViewBounds="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
By now, you should be able to understand each element in the XML file above. We have a
TextView which will display the text found inside the strings.xml resource file having the
name image_name. The ImageView will display an image (without any display options set such
as scaling and tinting). Below is the strings.xml resource file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="image_name">Display default image</string>
<string name="app_name">Image Displayer</string>
</resources>
When running our application, the following screen should be displayed:
3
Figure 8.1 Image Displayed in Android
To change the default display of the image, you could set a background (android:background
from XML or setBackgroundResource(int) from Java), padding (android:padding from XML or
setPadding(int,int,int,int) from Java), scale type to resize or move the image to match the size of
the ImageView (android:scaleType from XML or setScaleType(ImageView.ScaleType) from
Java), and other such properties. Play around with this properties on the application above to see
what effect they have on the displayed image.
For more details see the ImageView1 example that comes bundled with the Android SDK (found
at \android-sdk-windows-1.0_r2\samples\ApiDemos\src\com\example\android\apis\view\).
Video Playback
This section discusses a simple example of playing videos within a videoview object. For this
example you will need to push a video file in the specified folder. In this case, the
samplevideo.3gp needs to be pushed onto the sdcard folder you see in the DDMS perspective of
eclipse.
4
Video1\src\Video1.java:
package org.example.video;
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class Video extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fill view from resource
setContentView(R.layout.main);
VideoView video = (VideoView) findViewById(R.id.video);
// Load and start the movie
video.setVideoPath("/sdcard/samplevideo.3gp");
video.start();
}
}
5
Video1\res\layouts\main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="@+id/video"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
6