[Translation] audio and video

Source: Internet
Author: User
The Android platform has built-in codecs for some common media formats. In addition, the application can easily access the multimedia functions of the platform-you do so using the same intents and activities mechanic that the rest of Android uses.

Android allows you to play multiple audio and video data sources. You can play and store it in the appProgramMultimedia files in raw resources, multimedia files stored in the file system, or data streams read from the network. UseMediaplayerClass to play audio and video media.

When supported by hardware, Android allows you to record audio and video. Recording needs to be usedMediarecorderClass. Note that the recording cannot be performed on the simulator.

For the built-in media formats supported by Android, see the media format appendix of Android.

1. play audio and video

Multimedia data can be played from anywhere: Play back multimedia resources in raw resources, play back multimedia files in the file system, or play back multimedia data stored in the network by specifying a URL.

You can only play audio data to the standard output device. Currently, it is a mobile device speaker or Bluetooth headset. You cannot play audio files during a call. You cannot play sound files in the conversation audio.

1.1 play back multimedia files in raw resources

The most common scenario is playing media files (especially audios) in your own applications, which is very easy to do:

1.1.1 place multimedia resource files in the projectRes/rawDirectory, and then eclipse plugin (or aapt) will automatically generate an index value representing the media resource file in the r class.

1.1.2 import the index value representing the media resource fileMediaplayer. Create (context, int resid)Method, get a mediaplayer instance, and then call thisMediaplayerInstanceStart ()Method:

 
Mediaplayer MP=Mediaplayer. Create (context, R. Raw. sound_file_1 );
MP. Start ();

CallMediaplayer. Stop ()Method To Stop playback. If you want to load the new multimedia resource after a while, you must ensure thatMediaplayer. Start ()CallMediaplayer. Reset ()Method andMediaplayer. Prepare ()Method. (Mediaplayer. Create ()The method is called internally.Mediaplayer. Prepare ()Method ).

To pause playback, callMediaplayer. Pause ()Method, call againMediaplayer. Start ()The method resumes playing from the paused position.

1.2 play a file or stream

You can also play a media file in a file system or a media stream represented by a network URL:

1.2.1 createMediaplayer.

1.2.2 callMediaplayer. setdatasource ()And pass in the path of the media file (local file system or network URL ).

1.2.3 callMediaplayer. Prepare ()And then callMediaplayer. Start ()Method to start playing:

Mediaplayer MP= NewMediaplayer ();
MP. setdatasource (path_to_file );//Or web URL
MP. Prepare ();
MP. Start ();

Mediaplayer. Stop ()AndMediaplayer. Pause ()The functions of the method are the same as those before.

Note: MP may be null, so check whether MP is null after the new operator. In additionMediaplayer. setdatasource ()Method, it may be thrown because the specified file does not exist.IllegalargumentexceptionAndIoexceptionException.

Note: If you callMediaplayer. setdatasource ()Method to specify a network URL, the media file represented by this network URL must support multipart download.

1.3 play the jet content

The Android platform contains a jet engine, which allows you to add interactive playback of jet audio content to applications. You can use the jetcreator creation program included in the SDK to create the jet content for interactive playback. To play and manage jet content in a program, useJetplayerClass.

Concepts and usage of JetJetcreatorTool tutorial, you can viewJetcreatorUser Manual. In OS X and Windows systems, all functions of this tool are available. In Linux, all creation functions of this tool are supported, but the imported resources cannot be listened.

The following is an example of setting up Jet playback for A. Jet file stored on the SD card.Code:

 Jetplayer myjet  =  Jetplayer. getjetplayer ();
Myjet. loadjetfile ( " /Sdcard/level1.jet " );
Byte Segmentid = 0 ;

// Queue segment 5, repeat once, use general Midi, transpose by-1 Ave ave
Myjet. queuejetsegment ( 5 , - 1 , 1 , - 1 , 0 , Segmentid ++ );
// Queue Segment 2
Myjet. queuejetsegment ( 2 , - 1 , 0 , 0 , 0 , Segmentid ++ );

Myjet. Play ();

This SDK includes a sample application ――Jetboy-This example shows how to use jetplayer in a game to create interactive game music. It also explains how to use the jet event to synchronize music and game logic. The application is located in<SDK>/platforms/Android-1.5/samples/jetboy.

2. Audio collection

Audio Acquisition on the device is a little more complex than playing audio/video, but it is still quite simple:

1) use the new operator to createAndroid. Media. mediarecorderNew instance

2) createAndroid. content. contentvaluesInstance, and load standard attributes to it, suchTitle(Title ),Timestamp(Timestamp) and all importantMime_type.

3) create a path for storing the recording file (you can useAndroid. content. contentresolverCreate an entry in the database, and get it to assign a path automatically which you can then use)

4) CallMediarecorder. setaudiosource ()Method to specify the audio source. You may want to useMediarecorder. audiosource. Mic.

5) UseMediarecorder. setoutputformat ()Method To set the output file format.

6) UseMediarecorder. setaudioencoder ()Method To set the Audio Encoder.

7) CallMediarecorder. Prepare ()Method.

8) CallMediarecorder. Start ()Method To start audio collection.

9) CallMediarecorder. Stop ()Method To stop audio collection.

10) if you do not use thisMediarecorderWhen you callMediarecorder. Release ()Method.

2.1 sample code: audio collection

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

If (Newuri = Null ){
// Need to handle exception here-we were not able to create a new
// Content entry
}

String path = Contentresolver. getdatafilepath (newuri );

// Cocould use setpreviewdisplay () to display a preview to suitable view here

Recorder. setaudiosource (mediarecorder. audiosource. Mic );
Recorder. setoutputformat (mediarecorder. outputformat. three_gpp );
Recorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
Recorder. setoutputfile (PATH );

Recorder. Prepare ();
Recorder. Start ();

2.2 stop recording

The following code is an example of how to stop audio collection based on the preceding sample code:

 
Recorder. Stop ();
Recorder. Release ();

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.