Friday, April 18, 2014

android video recording method (from Andrews Flight Network)


There are classes associated with the android SDK: android.media.MediaRecorder
Of course, because the simulator does not provide the necessary hardware, so the learning process can not be achieved.
Media files can be played from anywhere: a practical resource file, a file system or a network link is available.

Here's how to play an audio media available in your application:
1, the file into the res / raw file in your project folder, in this folder, Eclipse plug-in will find it at the same time, this resource will be with your R
Class associated with it;
2, create a MediaPlayer, and resources associated with the use of MediaPlayer.create up after using the start () method in the instance.
For 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 re-use
play later this media, you must use the start () method again beforereset () method and prepare () methods to manipulate MediaPlayer object. (Create () first call to prepare ())
If you want to pause playback, you can use the pause () method. Where you pause resume playback function start () method can be realized.

Play a file
Here's how to play a file:
1 Create a MediaPlayer instance with the new;
2, call setDataSource () method, this method has a parameter of type String, the String type parameter contains the
you want to playPath to the file - the local file system or a URL;
3, after the first call to prepare () method, and then the start () method.
For example:
MediaPlayer mp = new MediaPlayer ();
mp.setDataSource (PATH_TO_FILE);
mp.prepare ();
mp.start ();
One thing to note: If you pass a file URL way, then the file must be downloaded and is uninterrupted, simply
That is to be played at the same time with the download.

Here's how to record audio media resources:
1, using the new to create an instance android.media.MediaRecorder;
2, create an instance and set the number of standard android.content.ContentValues ​​properties like TITLE, TIMESTAMP, the most important is MIME_TYPE;
3, create a path to the file to be placed, which can be android.content.ContentResolver in the content database to create a entry
Mouth, and automatically obtain this mark a file path.
4, using MediaRecorder.setAudioSource () method to set the audio resources; This will most likely use to MediaRecorder.AudioSource.MIC;
5, using MediaRecorder.setOutputFormat () method to set the output file format;
6, with MediaRecorder.setAudioEncoder () method to set the audio encoding;
7 Finally, prepare () and start () the recorded audio, stop () and release () call when you want to end.

Here is an example:
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 contentResolver = new ContentResolver ();
Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert (base, values); / / in the URL given to insert a column to a table of data
/ / Function prototypes: final Uri insert (Url, ContentValues ​​values);
if (newUri == null) {
/ / Exception handling needed here, we can not create a new content portal
here}
String path = contentResolver.getDataFilePath (newUri);
/ / You can use setPreviewDisplay () to display a preview to make suitable
Viewrecorder.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:
recorder.stop ();
recorder.release ();

In the process of recording audio resources, the use of this class to ContentValues ​​below to explain the class.
ContentValues ​​This class is used to store a series of values, these values ​​ContentResolver can process the requests.
The initial value given to create an empty series of values ​​ContentValues ​​(int size) constructor.
ContentValues ​​(ContentValues ​​from) This constructor creates a medium from the given ContentValues ​​to copy the values ​​generated.
This class has the following common methods:
void clear () to delete all values ​​
boolean containsKey (String key) If this object has been named value returns true
int describeContents () Description Value Type
Object get (String key) to obtain values ​​
void put (String key, Integer value) to add a value to the corresponding set of

There is a class ContentResolver, this class would like the content model provides application data.

This article flights from Andrews (android developer technical community)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Thank you to share
Reply:
Learn. . . . . . . . .

No comments:

Post a Comment