Android multimedia and camera details 1

Source: Internet
Author: User

The Android Multimedia Framework provides the ability to retrieve and play back audio, video, and various types of images, so you can easily integrate them into your applications. you can play audio or video from files stored in resources, files in the file system, or network data streams. These are all implemented using MediaPlayer or JetPlayerAPI. you can also use MediaRecorderand Camera APIs to record sound, video, or capture images.


The following topic demonstrates how to use the Android framework for multimedia retrieval and playback.

MediaPlayer

How to play audio and video in your application.

JetPlayer

How to Use the content created through JetCreator to play interactive audio and video.

Camera

How to use a camera on a device in your application to obtain images and videos.

AudioCapture

How to record sound in your application.

Media Playback

Android Multimedia Framework supports playing many common media types, so you can use MediaPlayerAPI to easily integrate audio and video and images into your application. you can play audio and video from resources, files, and networks.

 

This document demonstrates how to write a media playback application and interact with users and systems to achieve the best performance and user experience.

 


Note: You can only play audio and video to the standard output device. Currently, they are speakers or Bluetooth headsets. You cannot play audio files during phone calls.

Basic

The following types are used to play audio and video:

MediaPlayer

This is the main API for playing audio and video.

AudioManager

This class manages the audio source and output on the device.

Manifest Declaration

 

 

Before using MediaPlayer for development, make sure that your manifest declares the features that can be used.

InternetPermission-if you use MediaPlayer to play the content in the network stream, your application must request network access permissions.

<Uses-permissionandroid: name = "android. permission. INTERNET"/>

WakeLock Permission-you must request this Permission if your playback application needs to prevent the screen from going dark or sleep by the processor, or use the MediaPlayer. setScreenOnWhilePlaying () or MediaPlayer. setWakeMode () method.

<Uses-permissionandroid: name = "android. permission. WAKE_LOCK"/>

Use MediaPlayer


One of the most important components in a media framework is the MediaPlayer class. This type of object can be obtained, decoded, and played with a small amount of settings. It supports multiple media sources, such:

Local resources.

Internal URI, such as the URI you obtained from ContentResolver.

External URI (Streaming Media)

For a list of Media types supported by Android, see AndroidSupported Media Formats.

 

The following is an example of how to play audio from a local resource (saved in the res/raw/folder of your application ):

 

[Java]
MediaPlayer mediaPlayer = MediaPlayer. create (context, R. raw. sound_file_1 );
MediaPlayer. start (); // you do not need to call prepare (); create ()

MediaPlayer mediaPlayer = MediaPlayer. create (context, R. raw. sound_file_1 );
MediaPlayer. start (); // you do not need to call prepare (); create ()

 

In this example, the "raw" resource is a file that the system does not analyze in some way. however, the content of this resource cannot be the original audio. It should be a media file that is properly encoded and formatted (of course, in a supported format ).

 

 

The following is an example of how to play a local URI (URI is obtained by ContentResolver ):

 

[Java]
Uri myUri =...; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer ();
MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
MediaPlayer. setDataSource (getApplicationContext (), myUri );
MediaPlayer. prepare ();
MediaPlayer. start ();

Uri myUri =...; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer ();
MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
MediaPlayer. setDataSource (getApplicationContext (), myUri );
MediaPlayer. prepare ();
MediaPlayer. start ();

Playing from a remote URL based on an HTTP stream looks like this


[Java]
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 ();

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 ();


Note: If you input a URL to stream an online file, this file must be prefixed to download progressivedownload.

 

Warning when setDataSource () is used, you must capture and pass IllegalArgumentException and IOException, because the file you reference may not exist.

Asynchronous Preparation

 

MediaPlayer can be simple and direct in essence. however, there are still some important things to remember for a typical android Application. for example, a prepare () call may take a long time because it may need to obtain and enable the decoded media data. therefore, because some methods will be executed for a long time, you cannot call it from the UI thread of your application. otherwise, the UI will be suspended until this method is returned. this is a poor user experience and may lead to an ANR (no response from the application) error. even if you think that your resources are quickly loaded, remember that anything that takes more than 10 seconds on the interface will cause a significant pause and slow user impressions on your applications.

 

To avoid suspending the UI thread, another thread should be generated to "prepare" MediaPlayer and the main thread should be notified upon completion. however, although you can write the logic in the thread in person, you are more often using the framework to provide a convenient way: Using prepareAsync (). this method starts the "prepare" Process in the background and returns immediately. when the media "Preparation" is complete, MediaPlayer. the OnPreparedListener onPrepared () method (set by setOnPreparedListener () is called.

Management Status


Another aspect to remember about MediaPlayer is "status-based ". that is, the MediaPlayer has an internal state, because a specific operation is only valid in a specific State, so you must always pay attention to its changes when writing code. if you perform an operation in the wrong state, the system may throw an exception or cause an unexpected action.

 


MediaPlayer documents show a complete state legend, which clarifies which method enables MediaPlayer to change from one state to another. for example, when you create a new MediaPlayer, it is in the Idle state. at this time, you should call setDataSource () to initialize it, so that it enters the "initialized" state. then you should use prepare () or prepareAsync () "prepare" it. after the MediaPlayer preparation is complete, it enters the Prepared state, which means you can call start () to play the video. as shown in the following table, you can call start (), pause (), seekTo (), and other methods to convert the status of the MediaPlayer between Started, Paused, and PlaybackCompleted. when you call stop (), note that you cannot call start () again unless you re-prepare MediaPlayer.

 

When writing code to interact with the MediaPlayer, always remember the status change diagram of the MediaPlayer, because calling the method in the wrong state is the cause of common bugs.


Author: nkmnkm
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.