File Extensions

The extension is removed, so files with the same base name but different extensions will cause issues. You wouldn't want to put a file named goodmorningandroid.mp3 and another file named goodmorningandroid.m4a in there. Instead, if you would like to provide the same audio in multiple formats, it would be better if you included the format as part of the file name so that you can differentiate between them and the Android Developer Tools doesn't have problems generating the resource ID. If you...

Building a Time Lapse Photography

We have all seen beautiful examples of time-lapse photography. It is the process of taking several pictures over an even course of time. It could be one per minute, one per hour, or even one per week. Looking through a series of time-lapse photographs, we can see how something changes over time. One example might be watching a building being constructed, another might be documenting how a flower grows and blooms. Now that we have built a timer-based camera app, updating it to be a time-lapse...

Custom Audio Capture

Of course, using an intent to trigger the sound recorder isn't the only way we can capture audio. The Android SDK includes a MediaRecorder class, which we can leverage to build our own audio recording functionality. Doing so enables a lot more flexibility, such as controlling the length of time audio is recorded for. The MediaRecorder class is used for both audio and video capture. After constructing a MediaRecorder object, to capture audio, the setAudioEncoder and setAudioSource methods must...

Acknowledgments

The idea for this book came out of my work teaching at NYU. A huge debt of gratitude is owed to the ever encouraging faculty, staff, and students who comprise NYU's Interactive Telecommunications Program and who provide an endless source of inspiration. Thank you to Red Burns for creating, fostering, and ever improving ITP. Thank you to Dan O'Sullivan for constantly challenging me. Thank you to Tom Igoe and Dan Shiffman for showing me that it can be done. Thank you to Rob Ryan and Marianne...

About the Author

Shawn Van Every runs a mobile and streaming media consultancy to help companies better utilize emerging technologies related to audio and video with a focus on mobile and streaming applications. His clients have ranged from 19 Entertainment, MoMA, and Disney to Morgan Stanley, Lehman Brothers, and NYU Medical School, along with countless start-ups and other small clients. Additionally, Shawn is an Adjunct Assistant Professor of Communication in NYU's Interactive Telecommunications Program. His...

About the Technical Reviewers

Steve Bull has been coding and manipulating mobile devices since his days at Paul Allen's Interval Research in Palo Alto. As a mixed-media technology artist and entrepreneur, for the last nine years Bull has created location-specific narratives and games that explore the social, technological and creative possibilities of cell phones. He can be reached at www.stevebull.org. Wallace Jackson is a seasoned multimedia producer and i3D programmer for Acrobat3D PDF, Android mobile apps, iTV Design,...

Web Services

We are all familiar with web pages and the sites that encompass them, such as Yahoo, Google, Hulu, and Apress.com. What you may not be as familiar with is the concept of a web service. Simply put, a web service is a means to access the content and services offered by a web site in a programmatic fashion. Sites will enable their content to be accessed in this manner to allow third-party developers such as ourselves the ability to embed their content and functionality into applications. For...

BuiltIn Fonts

Drawing text without being able to specify a font or style would be pretty limiting. Fortunately, the Paint class allows us to specify which font should be used by calling the setTypeface method and passing in a Typeface object. The Typeface class has a number of constants defined that represent the built-in fonts that come with the Android OS. These fonts were created by a company called Ascender (www.ascendercorp.com ) as part of their Droid suite of fonts. They are defined in the Typeface...

Media Recorder Audio Encoders

Following the setting of the output format, we can call setAudioEncoder to set the codec that should be used. The possible values are specified as constants in the MediaRecorder.AudioEncoder class and other than DEFAULT, only one other value exists MediaRecorder.AudioEncoder.AMR_NB, which is the Adaptive Multi-Rate Narrow Band codec. This codec is tuned for speech and is therefore not a great choice for anything other than speech. By default it has a sample rate of 8 kHz and a bitrate between...

Drawing Text

A String and the start x and y coordinates along with a Paint object. The Paint class has a method called setTextSize for setting the size of the text that we can use. Paint paint new Paint paint.setColor Color.GREEN paint.setTextSize 40 float text_x 120 float text_y 120 canvas.drawText Hello, text_x, text_y, paint

Image Storage and Metadata

Android has a standard way to share data across applications. The classes responsible for this are called content providers. Content providers offer a standard interface for the storage and retrieval of various types of data. The standard content provider for images as well as audio and video is the MediaStore. The MediaStore allows the setting of the file in a standard location on the device and has facilities for storing and retrieving metadata about that file. Metadata is data about data it...

Putting It All Together

The following code is written to run on Android 2.2 and higher, but with minor changes, it should run on versions 1.6 and higher. The sections that require higher than 1.6 are noted with comments. package import java.io.FileNotFoundException import java.io.IOException import java.io.OutputStream import java.util.Iterator import java.util.List import android.content.ContentValues import android.content.res.Configuration import import...

Video Size

The width and height of the video as it is captured can be controlled with the setVideoSize method by passing in an integer for the width and the height in pixels. Standard sizes range from 176x144 to 640x480, with many devices supporting even higher resolutions. NOTE Many Android devices also support 720x480, but it should noted that this isn't the same pixel aspect ratio that would be captured by a DV camera, which captures rectangular pixels at 720x480. On Android devices, the pixels are...

Obtaining an URI for the Image

To obtain the standard location for storage of images, we first need to get a reference to the MediaStore. To do this, we use a content resolver. A content resolver is the means to access a content provider, which the MediaStore is. By passing a specific URI, the content resolver knows to provide an interface to the MediaStore as the content provider. Since we are inserting a new image, the method we are using is insert and the URI that we should use is contained in a constant in the class...

Media Recorder Output and Recording

Last, we'll want to call setOutputFile with the location of the file we want to record to. The following snippet of code creates a file using File.createTempFile in the preferred file location for applications that need to store files on the SD card. audioFile .3gp, path Now we can actually call prepare, which signals the end of the configuration stage and tells the MediaRecorder to get ready to start recording. We call the start method to actually start recording. recorder.prepare...

Introduction to Audio on Android

Any smartphone worth its name these days has audio playback capabilities on par with dedicated portable media devices or MP3 players. Of course, Android-based devices are no different. These capabilities allow for the building of music player, audio book, podcast, or just about any other type of application that is centered around audio playback. In this chapter, we'll explore what Android's capabilities are in terms of format and codec support, and we'll build a few different playback...

Pre and Post

Matrix Postrotate

Of course, these are just the tip of the iceberg. There are several more that may prove to be useful to you. Each of them also has a pre and a post version. These enable you to do more than one transformation at a time in sequence. For instance, you could do a preScale and then setRotate or a setScale and then postRotate. Changing the order of when they occur could yield vastly different results depending on the operations performed. Figure 3-11 shows the results of the following two method...

Supported Network Video Types

Android currently supports two different protocols for network delivered video. The first is media delivered via standard HTTP. As HTTP is broadly supported across networks and doesn't typically have problems with firewalls as other streaming protocols have had, a large amount of media is available in this manner. Media delivered via HTTP is commonly referred to as progressive download. Android supports on-demand media within MPEG-4 and 3GP files delivered from a standard web server via HTTP....

Output Format

Following the setting of the audio and video sources, we can set the output format using the MediaRecorder's setOutputFormat method, passing in the format to be used. The possible formats are listed as constants in the MediaRecorder.OutputFormat class. DEFAULT Specifies that the default output format will be used. This may be different across devices. On a Nexus 1 running Android 2.2, it is MPEG-4, the same format used if the MPEG_4 constant is used. MPEG_4 Specifies that the audio and video...

Controlling Playback

The MediaPlayer class has several nested classes that are interfaces for listening to events that the MediaPlayer sends. These events relate to state changes. For instance, the MediaPlayer will call the onCompletion method on a class that implements the OnCompletionListener and is registered via the setOnCompletionListener. This will be done when an audio file is done playing. Here is a full example of an activity that infinitely repeats, playing the same audio file by using the...

Playback Using an Intent

Onclicklistener Video

As with most of Android's capabilities that we have explored in this book, simply playing back a video can be done easily, using an intent to trigger the built-in Media Player application's playback activity. For this example, I captured a video using QuickTime X on a Mac laptop with a built-in iSight. I exported this video using QuickTime X's Save As command and selected iPhone as the format. This created a video that I named Test_Movie.m4v. The .m4v extension was given by QuickTime....

Creating a Custom Audio Playing Application

Of course, we aren't limited to using Android's built-in application for audio playback. We can write our own application that offers playback capabilities and more. To enable this, Android includes a MediaPlayer class. This class is used for the playback and control of both audio and video. Right now we'll just be using the audio playback capabilities. The simplest MediaPlayer example is to play back an audio file that is packaged with the application itself. In order to do that, an audio file...

Building a Timer Based Camera

To rectify the situation just described, we can add a time delay to the taking of the picture. Let's update our SnapShot example so that picture is taken ten seconds after pushing a button. In order to accomplish this, we'll need to use something like a java.util.Timer. Unfortunately, in Android, using a Timer creates some complications as it introduces a separate thread. To have separate threads interact with the UI, we need to use a Handler to cause an action to occur on the main thread. The...

Custom Video Capture

With images and audio there are multiple ways to do capture. Video capture is no exception we don't have quite as many options as we do with audio, but we do have the ability to create a custom video capture example using the MediaRecorder class. In many ways, building a custom video capture application is a lot like building a custom camera application combined with a custom audio recording application. We have to create a SurfaceView for the camera to draw preview or viewfinder images on,...

Billie Holiday

Albums listed in basic ListView To allow the user to actually select one of the albums, we need to override the default onListItemClick method that is provided by our ListActivity parent class. protected void onListItemClick ListView l, View v, int position, long id if currentState STATE_SELECT_ALBUM When an album in the list is selected, this method will be called. Since our currentState variable starts off as STATE_SELECT_ALBUM the first time this method is called it should be...

Retrieving Images Using the Media Store

One example that shows the power of using shared content providers on Android is the ease with which we can use them to create something like a gallery application. Because the content provider, in this case the MediaStore, is shared between applications, we don't need to actually create a camera application and a means to store images in order to make our own application to view images. Since most applications will use the default MediaStore, we can leverage that to build our own gallery...

Video Capture

In previous chapters, we have covered image capture and audio capture. Now we'll turn our attention to video capture. In this chapter, we'll explore capturing video using Android's built-in Camera application via an intent. We'll look at the formats and codecs that Android supports for video capture, and finally we'll build a custom video capture application. It is becoming clich , but as previously discussed, often the quickest and easiest way to perform some function on Android is to leverage...

Touch Events

Android Imageview Touch Event

To start this application, we'll need to understand how Android tells us when a user has touched the touchscreen. Being able to handle that, we can then allow the user create a drawing using his or her fingers on the touchscreen. As we know, many of the UI elements that we use in Android derive from the View class. Since we have been working with the Canvas from a Bitmap that is displayed in an ImageView, it makes sense to see if that can help us out with detecting where the user touches. As...

Selecting Images Using the BuiltIn Gallery Application

New Intent Intent

As we know, using an intent is often the quickest way to harness a capability that already exists in a pre-installed Android application. For the purposes of the examples in this chapter, let's look at how we can harness the built-in Gallery application to select an image that we might want to work with. The intent that we'll want to use is a generic Intent.ACTION_PICK that signals to Android that we want to select a piece of data. We also supply a URI for the data that we want to pick from. In...

Maximum File Size

The maximum size of a file captured by MediaRecorder can be specified by passing in the maximum size in bytes to the setMaxFileSize method. recorder.setMaxFileSize 10000000 10 megabytes To determine when maximum file size has been reached, we need to implement the MediaRecorder.OnInfoListener in our activity and register it with our MediaRecorder. The onInfo method will then be called and the what parameter can be checked against the constant. If they match,the maximum file size was reached....

External Fonts

We aren't limited in our Android applications to just the built-in fonts. Android supports the creation of Typeface objects from any TrueType font file. TrueType fonts are a standard and work on a variety of platforms. This opens up a wide range of possibilities for our applications. Many sites on the Internet offer free fonts, and, of course, there are font foundries, companies that create fonts that will sell you a license to use their fonts. One font that I found that was completely...

Enter the Matrix

The Android API has a Matrix class, which can be used when drawing on existing Bitmap objects or creating a Bitmap object from another Bitmap object. This class allows us to apply a spatial transformation to an image. A transformation of this type would be rotating, cropping, scaling, or otherwise altering the coordinate space of the image. The Matrix class represents transformations with an array of nine numbers. In many cases, these can be generated by a formula that mathematically represents...

Updating Our Camera Activity to Use Media Store for Image Storage and to Associate Metadata

The following is an update to our previous example, which saves our image in the MediaStore and then presents us with an opportunity to add a title and description. In addition, this version has several UI elements whose visibility is managed based upon the progress of the user in the application. package import java.io.FileNotFoundException import android.app.Activity import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri...

State Machine

As just described and as with the MediaPlayer object, the MediaRecorder has various states, and certain methods can be called only when in the appropriate state. Figure 111 is an illustration of the possible states and what state it moves to when specific methods are called reproduced again here for reference . MediaRecorder state diagram Figure 11-1. MediaRecorder state diagram from Android API Reference

Adding Controls with Media Controller

The VideoView has relatively few capabilities for controlling the playback of video. Specifically it has a start and a pause method. In order to provide more controls, we can instantiate a MediaController and set it via setMediaController to be the controller of the VideoView. The default MediaController has rewind, pause, play, and fast-forward buttons along with a scrubber and progress bar combination that can be used to seek to any point in the video. Here is an update to our VideoView...

Playing a Synthesized Sound

Here is a quick example showing how to construct an AudioTrack class and pass in data to play. For a full discussion of the parameters used to construct the AudioTrack object, please see the Raw Audio Playback with AudioTrack section of Chapter 7. This example uses an inner class that extends AsyncTask, AudioSynthesisTask. AsyncTask defines a method called doInBackground, which runs any code that is placed inside it in a thread that is separate from the main thread of the activity. This allows...

HTTP Audio Playback

The simplest case to explore would simply be to play an audio file that lives online and is accessible via HTTP. One such file would be this, which is available on my server Here is an example activity that uses the MediaPlayer to illustrate how to play audio available via HTTP. package import android.app.Activity import android.media.MediaPlayer import android.os.Bundle import android.util.Log public class AudioHTTPPlayer extends Activity MediaPlayer mediaPlayer public void onCreate Bundle...

Inserting Audio into the Media Store

Audio recordings may be put into the MediaStore content provider so they are available to other applications. The process is very similar to the process we used earlier to add images to the MediaStore. In this case though, we'll add them after they are created. We create a ContentValues object to hold the data that we'll insert into the MediaStore. A ContentValues object is made up of a series of key value pairs. The keys that may be used are defined as constants in the MediaStore.Audio.Media...

Media Recorder Example

Here is the code for a full custom audio capture and playback example using the MediaRecorder class. package import java.io.File import java.io.IOException import import android.view.View.OnClickListener Our CustomRecorder activity implements OnClickListener so that it may be notified when Buttons are pressed, and OnCompletionListener so that it can respond when MediaPlayer has completed playing audio. public class CustomRecorder extends Activity implements OnClickListener, OnCompletionListener...

Audio and Video Sources

Following instantiation, we can set the audio and video sources. To set the audio source, we use the setAudioSource method, passing in a constant representing the source we would like to use. The possible audio source values are constants defined in the CAMCORDER If the device has different microphones for use with different cameras front-facing, rear-facing , using this value will specify that the appropriate microphone is used Android 2.1 API level 7 or later . DEFAULT This specifies that the...

Capturing Sound for Analysis

As previously described, sound is vibration traveling through a substance. These vibrations can be captured by a microphone. Microphones convert the vibrations that travel through air into a constantly varying electrical current. When a microphone is used to capture sound by a computer, that sound is digitized. Specifically, amplitude samples of a specific size sample size are taken many times a second sample rate . This stream of data is called a PCM pulse code modulation stream, which forms...

Internal Metadata

EXIF, which stands for exchangeable image file format, is a standard way of saving metadata within an image file. Many digital cameras and desktop applications support the use of EXIF data. Since EXIF data is actually a part of the file, it shouldn't get lost in the transfer of the file from one place to another. For instance, when copying a file from the SD card of the Android device to a home computer, this data would remain intact. If you open the file in an application such as iPhoto, the...

Displaying Large Images

Imageview Android

Loading and displaying an image has significant memory usage implications. For instance, the HTC G1 phone has a 3.2-megapixel camera. A 3.2-megapixel camera typically captures images at 2048 pixels by 1536 pixels. Displaying a 32-bit image of that size would take more than100663kb or approximately 13MB of memory. While this may not guarantee that our application will run out of memory, it will certainly make it more likely. Android offers us a utility class called BitmapFactory, which provides...

Playback Using Video View

VideoView is a View that has video playback capabilities and can be used directly in a layout. It is very straightforward to use. The following layout XML file, main.xml, specifies a VideoView inside a LinearLayout. lt xml version 1.0 encoding utf-8 gt lt VideoView lt LinearLayout gt To utilize this VideoView, we simply have to gain a reference to it in the normal way, using findViewById, passing in the ID R.id.VideoView . Once we have the object, we can set the Uri to the video file with...

OnStart Command vs onStart

The onStartCommand method was introduced with Android 2.0 API level 5 . Previous to that, the method used was onStart. onStart's parameters are an intent and an int for startId. It does not include the int flags parameter and doesn't have a return. If you are targeting a phone that is running something earlier than 2.0, you can use the onStart method. public void onStart Intent intent, int startid The onDestroy method is called when the OS is destroying a Service. In this example, it is...

Returning Data from the Camera

Of course, simply capturing an image using the built-in camera application won't actually be useful without having the Camera application return the picture to the calling activity when one is captured. This can be accomplished by substituting the startActivity method in our activity with the startActivityForResult method. Using this method allows us the ability to access the data returned from the Camera application, which happens to be the image that was captured by the user as a Bitmap....

Using the BuiltIn Audio Player via an Intent

As with using the camera, the easiest way to provide the ability to play an audio file within an application is to leverage the capabilities of the built-in Music application. This application plays all of the formats that Android supports, has a familiar interface to the user, and can be triggered to play a specific file via an intent. The generic android.content.Intent.ACTION_VIEW intent with the data set to a Uri to the audio file and the MIME type specified allows Android to pick the...

Saving a Bitmap Based Canvas Drawing

What good would it be to just draw on an image without being able to save it after the user has created a masterpiece So far we have just drawn images let's look at how we can commit these wonderful drawings to permanence. Well, at least let's look at how we can save them to the SD card. Unsurprisingly, it will be a similar process to what we used in the second chapter to save images captured from our custom camera application. Let's go over the changes that we can make to our ChoosePictureDraw...

Text on a Path

Text isn't limited to being drawn on a horizontal line it can be drawn on a Path as well. Here is an example. Paint paint new Paint paint.setColor Color.GREEN paint.setTextSize 20 Path p new Path p.moveTo 20, 20 p.lineTo 100, 150 p.lineTo 200, 220 canvas.drawTextOnPath Hello this is text on a path, p, 0, 0, paint

Audio Capture with an Intent

Record Sound Action

The easiest way to simply allow audio recording capabilities in an application is to leverage an existing application through an intent that provides recording capabilities. In the case of audio, Android ships with a sound recorder application that can be triggered in this manner. The action used to create the intent is available as a constant called RECORD_SOUND_ACTION within the MediaStore.Audio.Media class. Here is the basic code to trigger the built-in sound recorder. Intent intent new...

Preview Surface

Also before we can get started using the camera, we need to create some type of Surface for the Camera to draw viewfinder or preview images on. A Surface is an abstract class in Android representing a place to draw graphics or images. One straightforward way to provide a drawing Surface is to use the SurfaceView class. SurfaceView is a concrete class providing a Surface within a standard View. To specify a SurfaceView in our layout, we simply use the lt SurfaceView gt element within any normal...

Media Recorder Output Formats

The next method to be called in sequence is setOutputFormat. The values this takes in are specified as constants in the MediaRecorder.OutputFormat inner class. This specifies that the file written will be an MPEG-4 file. It may contain both audio and video tracks. This represents a raw file without any type of container. This should be used only when capturing audio without video and when the audio encoder is AMR_NB. This specifies that the file written will be a 3GPP file extension .3gp . It...

Media Store for Audio

We explored using the MediaStore for images early on in this book. Much of what we learned can be leveraged for the storage and retrieval of other types of media, including audio. In order to provide a robust mechanism for browsing and searching for audio, Android includes a MediaStore.Audio package, which defines the standard content provider. Accessing audio files that are stored using the MediaStore provider is consistent with our previous uses of the MediaStore. In this case, we'll be using...

Media Recorder Audio Sources

The first method that should be called after the MediaRecorder is instantiated is setAudioSource. setAudioSource takes in a constant that is defined in the AudioSource inner class. Generally we will want to use MediaRecorder.AudioSource.MIC, but it is interesting to note that MediaRecorder.AudioSource also contains constants for VOICE_CALL, VOICE_DOWNLINK, and VOICE_UPLINK. Unfortunately, it appears as though there aren't any handsets or versions of Android where recording audio from the call...

Video Thumbnails from the Media Store

We could, starting with Android 2.0 API Level 5 , pull out the thumbnails associated with each video file from within the loop. We need the ID of the video file that is in our list of columns to select MediaStore.Video.Media._ID , which we can then use in the where clause of the managedQuery. int id String thumbColumns MediaStore.Video.Thumbnails.DATA, Cursor thumbCursor thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID id, null, null if thumbCursor.moveToFirst

Audio and Video Bitrates

Video encoding bitrates can be set using the setVideoEncodingBitrate method on the MediaRecorder and passing in the bitrate requested in bits per second. A low bitrate setting for video would be in the range of 256,000 bits per second 256 kbps , while a high bitrate for video would be in the range of 3,000,000 bits per second 3 mbps . We can also specify the maximum bitrate that we would like used for the encoded audio data. We do so by passing the value as bits per second into the...

Supported Audio Formats

Android supports a variety of audio file formats and codecs for playback it supports fewer for recording, which we'll discuss when we go over recording . AAC Advanced Audio Coding codec as well as both profiles of HE-AAC, High Efficiency AAC , .m4a audio m4a or.3gp audio 3gpp files. AAC is a popular standard that is used by the iPod and other portable media players. Android supports this audio format inside of MPEG-4 audio files and inside of 3GP files which are based on the MPEG-4 format ....

PrepareAsyncO Preparing

Looping true amp amp playback completes Looping true amp amp playback completes Looping false amp amp onCompletlonO invoked on OnCompletionListener Looping false amp amp onCompletlonO invoked on OnCompletionListener Figure 6-1. MediaPlayer state diagram from Android API Reference Figure 6-1. MediaPlayer state diagram from Android API Reference Here is a full MediaPlayer example that uses prepareAsync and implements several listeners to keep track of its state. package import java.io.IOException...

Visualizing Frequencies

One common way that people typically use to analyze audio is to visualize the frequencies that exist within it. Commonly these types of visuals are employed with equalizers that allow the adjustment of the levels of various frequency ranges. The technique used to break an audio signal down into component frequencies employs a mathematic transformation called a discrete Fourier transform DFT . A DFT is commonly used to translate data from a time base to a frequency base. One algorithm used to...

Raw Audio Capture and Playback Example

Here is a full example that records using AudioRecord and plays back using AudioTrack. Each of these operations lives in their own thread through the use of AsyncTask, so that they don't make the application become unresponsive by running in the main thread. package import java. import java. import java. import java. import java. import java. import java. import java. import android.view.View.OnClickListener public class AltAudioRecorder extends Activity implements OnClickListener We have two...

Adding Video Metadata

As we discussed in Chapter 9, Android's MediaStore content provider has a portion, MediaStore.Video, dedicated to video in addition to the portions for image and audio files and metadata that we have previously looked at. When triggering the Camera application via an intent, the Uri to the newly recorded video file that is returned is a content style Uri, which is used in combination with a content provider in this case, the MediaStore. In order to add additional metadata, we can use the Uri...

Image Capture Using the BuiltIn Camera Application

With mobile phones quickly becoming mobile computers, they have in many ways replaced a whole variety of consumer electronics. One of the earliest non-phone related hardware capabilities added to mobile phones was a camera. Currently, it seems someone would be hard pressed to buy a mobile phone that doesn't include a camera. Of course, Android-based phones are no exception from the beginning, the Android SDK has supported accessing the built-in hardware camera on phones to capture images. The...

Drawing on Existing Images

Since we are drawing on a Canvas, we can use techniques described in the previous chapter to draw an image to the Canvas and then draw on top of that image. package import java.io.FileNotFoundException import android.app.Activity import android.content.Intent import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Color import android.graphics.Matrix import android.graphics.Paint import android.net.Uri import android.os.Bundle...

Network Video Playback

Mediaplayer Onerrorlistener

Android supports HTTP and RTSP video playback in all three video playback methods discussed in Chapter 9. Using either the built-in Media Player activity via an intent or the VideoView class to play either form of network video requires no source code changes. Simply use the HTTP or RTSP URL as the video Uri, and it will work as long as the format is supported. Here is the ViewTheVideo activity example from Chapter 9 that uses a VideoView with an RTSP URL to a video from YouTube's mobile site....

Raw Audio Recording with Audio Record

Aside from using an intent to launch the sound recorder and using the MediaRecorder, Android offers a third method to capture audio, using a class called AudioRecord. AudioRecord is the most flexible of the three methods in that it allows us access to the raw audio stream but has the least number of built-in capabilities, such as not automatically compressing the audio. The basics for using AudioRecord are straightforward. We simply need to construct an object of type AudioRecord, passing in...

Drawing a Bitmap onto a Bitmap

Before we get into the specific mechanisms used to alter the images, let's look at how we can create a new, empty Bitmap object and draw an existing Bitmap into that. This is the process that we will be using to create altered versions of our images. In the foregoing example, we have a Bitmap object instantiated with an image that has been selected by the user. It has been instantiated by a call to BitmapFactory's decodeStream method, as we learned how to do in Chapter 1. Bitmap bmp...

Playback Using a Media Player

In Chapters 6 and 7, those dealing with audio and networked audio, we introduced the MediaPlayer class. The very same MediaPlayer class can also be used for video playback, in much the same manner. Using a MediaPlayer object for video playback gives us the greatest amount of flexibility in the control of the playback itself, as compared with playing video using VideoView or via an intent. In fact, the mechanism used to handle the actual playback within the VideoView and the activity triggered...

Other Media Recorder Methods

MediaRecorder has a variety of other methods available that we can use in relation to audio capture. getMaxAmplitude Allows us to request the maximum amplitude of audio that has been recorded by the MediaPlayer. The value is reset each time the method is called, so each call will return the maximum amplitude from the last time it is called. An audio level meter may be implemented by calling this method periodically. setMaxDuration Allows us to specify a maximum recording duration in...

Image Compositing

Android Porterduff Samples

Compositing is the act of putting together two images, allowing features of both images to be seen. In the Android SDK, we can accomplish compositing by first drawing one Bitmap to a Canvas and then drawing a second Bitmap to the same Canvas. The only difference is that we specify a transfermode Xfermode on the Paint when drawing the second image. The set of classes that can be used as a transfermode all derive from the Xfermode base class and include one called PorterDuffXfermode. The...

Generating Samples

Using a little bit of math, we can algorithmically create these samples. The classic sine wave can be reproduced. This example produces a sine wave at 440 Hz. package import android.view.View.OnClickListener public class AudioSynthesis extends Activity implements OnClickListener AudioSynthesisTask audioSynth boolean keepGoing false float synth_frequency 440 440 Hz, Middle A Override public void onCreate Bundle savedInstanceState super.onCreate savedInstanceState setContentView R.layout.main...

Audio Sample Rate

Along with bitrate, the audio sample rate is important in determining the quality of the audio that is captured and encoded. The MediaPlayer has a method, setAudioSampleRate, which allows us to request a specific sample rate. The sample rate passed in is in Hz hertz , which stands for the number of samples per second. The higher the sample rate, the larger the range of audio frequencies that can be represented in the captured file. A sample rate on the low end, 8,000 Hz, is suitable for...

Implementing the Camera

Android Effect Blackboard

Now that we have the activity and preview Surface all set up, we are ready to start using the actual Camera object. When the Surface is created, which will trigger calling the surfaceCreated method in our code due to the SurfaceHolder.Callback, we can obtain a Camera object by calling the static open method on the Camera class. public void surfaceCreated SurfaceHolder holder camera Camera.open We'll want to follow that up with setting the preview display to the SurfaceHolder we are using, which...

Android Media

Developing Graphics, Music, Video and Rich Media Apps for Smartphones and Tablets Developing Graphics, Music, Video, and Rich Media Apps for Smartphones Pro Android Media Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets Copyright 2009 by Shawn Van Every All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval...