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
- Mediaplayer MP =
New
Mediaplayer ();
- MP. setdatasource (path_to_file );
- MP. Prepare ();
- 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
- Recorder =
New
Mediarecorder ();
- Contentvalues values = new
Contentvalues (
3
);
- Values. Put (mediastore. mediacolumns. Title, some_name_here );
- Values. Put (mediastore. mediacolumns. timestamp, system. currenttimemillis ());
- Values. Put (mediastore. mediacolumns. mime_type, recorder. getmimecontenttype ();
- Contentresolver = new
Contentresolver ();
- Uri base = mediastore. Audio. internal_content_uri;
- Uri newuri = contentresolver. insert (base, values );
- // Insert a column of data into a table in the given URL
- // Function prototype: Final URI insert (URL, contentvalues values );
- If
(Newuri =
Null
){
- // Exception handling is required. We cannot create a new content entry here.
- }
- String Path = contentresolver. getdatafilepath (newuri );
- // You can use setpreviewdisplay () to display a preview to make the view fit
- Recorder. setaudiosource (mediarecorder. audiosource. Mic );
- Recorder. setoutputformat (mediarecorder. outputformat. three_gpp );
- Recorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
- Recorder. setoutputfile (PATH );
- Recorder. Prepare ();
- Recorder. Start ();
Stop recording:
Java code:
Copy to clipboard
C/C ++ code
- Recorder. Stop ();
- 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.