Android’s robust APIs can be used to add video, audio, and photo capabilities to app for playing and recording media. The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications.
1. Chapter 23
Media
By
Dr. Ramkumar Lakshminarayanan
Introduction
Android’s robust APIs can be used to add video, audio, and photo capabilities to app for
playing and recording media. The Android multimedia framework includes support for playing
variety of common media types, so that you can easily integrate audio, video and images into
your applications.
Media Playback
MediaPlayer APIs are used to play audio or video from media files stored in the
application's resources (raw resources), from standalone files in the filesystem, or from a data
stream arriving over a network connection.
You can play back the audio data only to the standard output device. Currently, that is the
mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation
audio during a call.
The following classes are used to play sound and video in the Android framework:
MediaPlayer- This class is the primary API for playing sound and
video.AudioManager - This class manages audio sources and audio output on a
device.
Manifest Declaration
Before starting development on your application using MediaPlayer, make sure your manifest
has the appropriate declarations to allow use of related features.
Internet Permission - If you are using MediaPlayer to stream network-based content, your
application must request network access.
Wake Lock Permission - If your player application needs to keep the screen from
dimming or the processor from sleeping, or uses the
<uses-permissionandroid:name="android.permission.INTERNET" />
2. MediaPlayer.setScreenOnWhilePlaying() or MediaPlayer.setWakeMode() methods, you
must request this permission.
Media Player
One of the most important components of the media framework is the
MediaPlayer class. An object of this class can fetch, decode, and play both audio and
video with minimal setup. It supports several different media sources such as:
• Local resources
• Internal URIs, such as one you might obtain from a Content Resolver
• External URLs (streaming)
Here is an example of how to play audio that's available as a local raw resource
(saved in your application's res/raw/ directory):
In this case, a "raw" resource is a file that the system does not try to parse in any
particular way. However, the content of this resource should not be raw audio. It should
be a properly encoded and formatted media file in one of the supported formats.
And here is how you might play from a URI available locally in the system (that
you obtained through a Content Resolver, for instance):
Playing from a remote URL via HTTP streaming looks like this:
Audio Capture
<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>
Uri myUri = ....;// initialize Uri here
MediaPlayermediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(),myUri);
mediaPlayer.prepare();
mediaPlayer.start();
Stringurl = "http://........";//yourURL here
MediaPlayermediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();//mighttake long!(forbuffering,etc)
mediaPlayer.start();
3. The Android multimedia framework includes support for capturing and encoding a
variety of common audio formats, so that you can easily integrate audio into your applications.
You can record audio using the MediaRecorder APIs if supported by the device hardware.
Audio capture from the device is a bit more complicated than audio and video playback,
but still fairly simple:
1.Create a new instance of android.media.MediaRecorder.
2.Set the audio source using MediaRecorder.setAudioSource(). You will probably want
to use MediaRecorder.AudioSource.MIC.
3.Set output file format using MediaRecorder.setOutputFormat().
4.Set output file name using MediaRecorder.setOutputFile().
5.Set the audio encoder using MediaRecorder.setAudioEncoder().
6.Call MediaRecorder.prepare() on the MediaRecorder instance.
7.To start audio capture, call MediaRecorder.start().
8.To stop audio capture, call MediaRecorder.stop().
9.When you are done with the MediaRecorder instance, call MediaRecorder.release() on
it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
Example: Record audio and play the recorded audio
The example class below illustrates how to set up, start and stop audio capture, and to
play the recorded audio file.
/* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to r/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*/
package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
7. Summary
In this unit we have discussed about the Media API in Android and set up, start and stop
audio capture, and to play the recorded audio file.