1. Media Palyback
The Android multimedia framework schemdes support for playing variety of common media types, so that you can easily integrate audio, video
And images into your applications.
You can play audio or video from media files stored in your application's resources (raw resources), from standalone files in the filesystem,
Or from a data stream arriving over a network connection, all using MediaPlayer APIs.
2. The Basic
MediaPlayer ---> Primary API for Playing sound and video
AudioManager ---> managers audio sources and audio output on a device
3. Manifest Declarations
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. WAKE_LOCK"/>: keep the screen from dimming or the processor from sleeping
4. Using MediaPalyer
An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources
Such:
<1> Local resources
Here is an simple example of how to play audio
<I> place a local resources saved in res/raw/directory
<Ii>
MediaPlayer mediaPlayer = MediaPlayer. create (getApplication (), R. raw. beatiful_girl); mediaPlayer. start ();
<2> Internal URIs, such as one you might obtain from a Content Resolver
Uri myUri = ....; // initialize Uri here, obtained through Content ResolverMediaPlayer mediaPlayer = new MediaPlayer (); mediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); mediaPlayer. setDataSource (getApplicationContext (), myUri); mediaPlayer. prepare (); mediaPlayer. start ();
<3> External URLs (streaming)
String url = "http ://........ "; // your URL hereMediaPlayer mediaPlayer = new MediaPlayer (); mediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC); mediaPlayer. setDataSource (url); mediaPlayer. prepare (); // might take long! (For buffering, etc) mediaPlayer. start ();
4.1 Asynchronous Preparation
Important to keep in mind:
<1> the call to prepare () can take a long time to excute, because it might involve fetching and decoding media data.
So, never call it from application's UI thread.
<2> framework supplies a convinient way to accomplish this task by using prepareAsync () method
When the media is done preparing, the onPrepared () method of the MediaPlayer. OnPreparedListener
, Configured through
SetOnPreparedListener () is called.
4.2 Managing State
4.3 Releasing the MediaPlayer
A MediaPlayer can consume valuable system resources. Therefore, you shocould call release () to make sure any system resources
Allocated toit are properly released
MediaPlayer. release (); mediaPlayer = null;
As an example, consider the problems that cocould happen if you forgot to releaseMediaPlayer
When your activity is stopped,
Create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes
Device configuration in another way), the system handles that by restarting the activity (by default), so you might quickly consume
All of the system resources as the user rotates the device back and forth between portrait and landscape, because at each
Orientation change, you create a new MediaPlayer
That you never release.
5. Using a Service with MediaPlayer
If you want your media to play in the background even when your application is not onscreen-that is, you want it to continue playing
While the user is interacting with other applications-then you must start a Service and control the MediaPlayer instance from there.
5.1 Running asynchronousyly
First of all, like an Activity, all work in a Service is done in a single thread by default.
If you are running an activity and a service from the same application ("main thread") by default,
Therefore, services needs to process incoming intents quickly and never perform lengthly computatipons when responding to them.
If any heavy work or blocking callare expected, you must do those tasks asynchronously:
Either from another thread you implement yourself, or using the framework's own facilities for asynchronous processing.
8. Media and Camera/Media Playback