标签:des android style blog http io color ar os
1.Media Palyback
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.
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 as:
<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 Resolver MediaPlayer 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 here MediaPlayer 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 should call release() to make sure any system resources
allocated toit are properly released
mediaPlayer.release(); mediaPlayer = null;
As an example, consider the problems that could happen if you forgot to release the MediaPlayer
when your activity is stopped, but
create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes the
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 calls are expected, you must do those tasks asynchronously:
either from another thread you implement yourself, or using the framework‘s many facilities for asynchronous processing.
8.Media and Camera/Media Playback
标签:des android style blog http io color ar os
原文地址:http://www.cnblogs.com/iMirror/p/4082447.html