8.Media and Camera/Media Playback

來源:互聯網
上載者:User

標籤: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 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 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.