Briefly describe the video formats supported by Android, as follows:
Different mobile phones support different encoding/decoding methods due to different hardware.
G1 supports Decoding in the WMA, WMV, and H.264 AVC formats. Android-supported audio/video encoding methods include: AMR-NB, H.263,
The output video format is *. 3GP or *. MP4, which should be noted in future development.
After learning about Android coding and decoding, let's study how to play/record videos on Android and open media instructions in Android SDK. The main parts of this abstract are as follows:
First Note: mediaplayer and mediarecoder are mainly used to play and record videos. Because these two classes are complex, this article will introduce in detail how to use mediarecoder, mediaplayer will be detailed later.
The other Classes define the encoding supported by android as follows:
Class |
Description |
Encoding method definition |
Mediarecorder. audioencoder |
Audio Encoding time |
Amr_nb: AMR Nb Encoding |
Mediarecorder. audiosource |
Sound Sampling Device |
Mic: microphone |
Mediarecorder. outputformat |
Recording output format |
Mpeg_4: *. MP4 |
Raw_amr: *. Amr |
Three_gpp: *. 3GP |
Mediarecorder. videoencoder |
Video Encoding Method |
H263: H.263 Encoding |
H264: H.264 Encoding |
Mpeg_4_sp: MP4 Encoding |
Mediarecorder. videosource |
Video Sampling Device |
Camera: digital cameras |
How to Use mediarecorder to record sound?
First, you should have a general understanding of sound recording. You need to set the sound data source, output encoding method, output file path, and output file format. One thing to note is that the output file format is equivalent to a container. You must specify the encoding format for the specific encoding. The same encoding may have different output formats, and the same output format may have different encoding methods.
Android. Media Contains classes for interacting with the media subsystem. Use Android. Media. mediarecorder
Class for media sampling, including audio and video. Mediarecorder
Run as a state machine. You need to set different parameters, such as the source device and format. After setting, you can execute recording for any duration until the user stops.
The main audio recordings are as follows:
Mediarecorder mrec;
File audiofile = NULL;
Private Static final string tag = "soundrecordingdemo ";
Protected void startrecording
() Throws ioexception
{
Mrec. setaudiosource (mediarecorder. audiosource. Mic );
Mrec. setoutputformat (mediarecorder. outputformat. three_gpp );
Mrec. setaudioencoder (mediarecorder. audioencoder. amr_nb );
If (msamplefile = NULL)
{
File sampledir = environment. getexternalstoragedirectory ();
Try
{
Audiofile = file. createtempfile ("IBM", ". 3GP", sampledir );
} Catch (ioexception E)
{
Log. E (TAG, "sdcard access error ");
Return;
}
}
Mrec. setoutputfile (audiofile. getabsolutepath ());
Mrec. Prepare ();
Mrec. Start ();
}
Protected void stoprecording
()
{
Mrec. Stop ();
Mrec. Release ();
Processaudiofile (audiofile. getabsolutepath ());
}
Protected void processaudiofile
()
{
Contentvalues values = new contentvalues (3 );
Long Current = system. currenttimemillis ();
Values. Put (mediastore. Audio. Media. title, "audio" + audiofile. getname ());
Values. Put (mediastore. Audio. Media. date_added, (INT) (current/1000 ));
Values. Put (mediastore. Audio. Media. mime_type, "audio/3GPP ");
Values. Put (mediastore. Audio. Media. Data, audiofile. getabsolutepath ());
Contentresolver = getcontentresolver ();
Uri base = mediastore. Audio. Media. external_content_uri;
Uri newuri = contentresolver. insert (base, values );
Sendbroadcast (new intent (intent. action_media_scanner_scan_file, newuri ));
}
The above code is parsed as follows:
- In the startrecording method, instantiate and initialize the mediarecorder instance.
- The input source is set to MIC ).
- The output format is set to 3GPP (*. 3GP file), which is a media format dedicated to mobile devices.
- The encoder is set to amr_nb. This is the audio format and the sampling rate is 8 kHz. NB indicates a narrow frequency. The SDK documentation explains different data formats and available encoders.
- Audio
Frequency files are stored on the memory card rather than in the memory. External. getexternalstoragedirectory ()
Return the name of the memory card location. A temporary file name will be created in this directory. Then, associate the file with mediarecorder by calling the setoutputfile method.
Instance. Audio data is stored in this file.
- Call the prepare method to initialize mediarecorder. The start method is called when you prepare to start the recording process. Before calling the stop method, the files on the memory card are recorded. The release method releases the resources allocated to the mediarecorder instance.
- After the audio is sampled, add the audio to the device's media library. In this sample code, the processaudiofile method adds the audio to the media library. Use intent to notify the media application on the device that new content is available.
- At last, you must note that the recording sound requires certain permissions. You need to add permission declarations to androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. record_audio"> </uses-Permission> summary:
- Android
The SDK provides great convenience for video development. However, during usage, it is found that these classes are encapsulated at a high level and cannot meet the needs of developers in many places, it is mainly reflected in the absence of a pair of Files
Stream operations and other underlying interfaces, we can only control the file stream by controlling the status. The lack of such operations increases the difficulty of Android video development, such as adding other encoders.
- Android
T-Mobile
G1 can watch YouTube videos online. However, during development, we found that videos in FLV format cannot be played. Why? It is estimated that it will be supported in the future. FLV is the most current stream.
Line streaming media format, if not supported, will have a great impact on users directly watching these FLV videos on Android.