Audio and video development in Android

Source: Internet
Author: User

The video recording function is coming soon. In Android
SDK
There are related classes: Android. Media. mediarecorder, of course, because the simulator does not provide the necessary hardware facilities, it cannot be implemented during the learning process. Media can play files from anywhere: an actual file resource, a file in the system, or an available network link.


The following describes how
To play an available media audio:



1. Put the file in the Res/raw folder of your project. In this folder, The Eclipse plug-in will find it, and the resource and your r


Classes are associated;

2. Create a mediaplayer and associate it with the resource using mediaplayer. Create. Then, use the START () method in the instance.

Example: mediaplayer MP = mediaplayer. Create (context, R. Raw. sound_file_1 );

MP. Start ();


 

If you want to stop playing, use the stop () method. If you want to play this media clip again later, you must use the reset () method and
The prepare () method to operate the mediaplayer object. (Create () calls prepare () for the first time.) If you want to pause playback, you can use pause ()
Method. You can use the START () method to Resume playback in the paused area.


Play a file

The following describes how to play a file:


1. Create a mediaplayer instance with new;

2. Call the setdatasource () method. This method has a string type parameter. This string type parameter contains the path of the file to be played-local file system or URL;

3. Call the prepare () method first, and then start () method.

Java
Code:


Copy to clipboard
Java code
  1. Mediaplayer MP =
    New
    Mediaplayer ();
  2. MP. setdatasource (path_to_file );
  3. MP. Prepare ();
  4. MP. Start ();


Note that if you pass a URL-based file, the file must be downloadable and uninterrupted, to put it simply, the video is downloaded at the same time.


The following describes how to record Media Audio resources:



1. Use new to create an instance Android. Media. mediarecorder;

2. Create an android. content. contentvalues instance and set some standard attributes, such as title and timestamp. The most important attribute is mime_type;

3. Create a path for the file to be placed. You can use Android. content. contentresolver to create an entry in the content database and automatically mark a path to obtain the file.

4. Use mediarecorder. setaudiosource () to set audio resources. This will probably use mediarecorder. audiosource. Mic;

5. Use mediarecorder. setoutputformat () to set the output file format;

6. Use mediarecorder. setaudioencoder () to set the audio encoding;

7. At last, the audio, stop () and release () recorded by prepare () and start () are called at the end.


The following is an example:

Java code:

Copy to clipboard
Java code
  1. Recorder =
    New
    Mediarecorder ();
  2. Contentvalues values = new
    Contentvalues (
    3
    );
  3. Values. Put (mediastore. mediacolumns. Title, some_name_here );
  4. Values. Put (mediastore. mediacolumns. timestamp, system. currenttimemillis ());
  5. Values. Put (mediastore. mediacolumns. mime_type, recorder. getmimecontenttype ();
  6. Contentresolver = new
    Contentresolver ();
  7. Uri base = mediastore. Audio. internal_content_uri;
  8. Uri newuri = contentresolver. insert (base, values );
  9. // Insert a column of data into a table in the given URL

  10. // Function prototype: Final URI insert (URL, contentvalues values );

  11. If
    (Newuri =
    Null
    ){
  12. // Exception handling is required. We cannot create a new content entry here.

  13. }
  14. String Path = contentresolver. getdatafilepath (newuri );
  15. // You can use setpreviewdisplay () to display a preview to make the view fit

  16. Recorder. setaudiosource (mediarecorder. audiosource. Mic );
  17. Recorder. setoutputformat (mediarecorder. outputformat. three_gpp );
  18. Recorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
  19. Recorder. setoutputfile (PATH );
  20. Recorder. Prepare ();
  21. Recorder. Start ();


Stop recording:


Java code:

Copy to clipboard
C/C ++ code
  1. Recorder. Stop ();
  2. Recorder. Release ();




The contentvalues class is used in the process of recording audio resources. The class is described below.

The contentvalues class is used to store a series of values that require contentresolver to be able to process.

The contentvalues (INT size) constructor uses the given initial value to create an empty series value.

The contentvalues (contentvalues from) constructor creates a value for copying from a given contentvalues.


This class has the following common methods:


Void clear () delete all values

Boolean containskey (string key) returns true if this object has a named value.

Int describecontents () Description Value Type

Object get (string key) Get value

Void put (string key, integer value) adds a value to the corresponding set.

Another class is contentresolver, which provides application data for the content model.

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.