[Streaming media] Android real-time video collection-mediarecoder recording

Source: Internet
Author: User
ArticleDirectory
    • ① Use mediarecoder
    • ② Use localsocket
    • ③ Mediarecorder Problems
[Streaming media] Android real-time video collection-mediarecoder recording

Skyseraph Mar 31st 2012

Email: zgzhaobo@gmail.com QQ: 452728574

0

This article is only a series of coherence. In fact, mediarecoder has been used in the previous series of blog posts. For details, see the previous Android series blog Android (19)

1 Overview

Use mediarecorder of Android to bind localsocket to the setoutputfile function.

2 knowledge points ① use mediarecoder

Reference [Questions about Android] multi-media: mediarecorder enables a recorder

② Use localsocket

Send and receive data simultaneously on a mobile phone.

Http://api.apkbus.com/reference/android/net/LocalSocket.html

Http://api.apkbus.com/reference/android/net/LocalServerSocket.html

3 core source code ① surfaceview

Reference[Streaming media] Android real-time video collection-camera preview collectionMulti-media: mediarecorder implements a recorder

② Init localsocket
Private VoidInitlocalsocket ()
{
Extends ER =NewLocalsocket ();
Try
{
LSS =NewLocalserversocket ("h264 ");
Receiver. Connect (NewLocalsocketaddress ("h264 "));
Receiver. setreceivebuffersize (500000 );
Receiver. setsendbuffersize (500000 );
Sender = lss. Accept ();
Sender. setreceivebuffersize (500000 );
Sender. setsendbuffersize (500000 );
}Catch(Ioexception E1)
{
E1.printstacktrace ();
Log. E ("", "localsocket error:" + e1.getmessage ());
}
}
③ Mediarecorder Problems

Reference:[Questions about Android] multi-media: video recorder implemented by mediarecoder  Multi-media: mediarecorder implements a recorder

The initiativevideo core module is attached.

 Private   Boolean Initializevideo ()
{
Try
{
Log. I (TAG, "# initializevideo ....");
// 〇 state: initial instantiate the mediarecorder object
If (Msurfaceview =Null )
{
Log. E (TAG, "msurfaceview is null in initializevideo ");
Return False ;
}
If (Mmediarecorder = Null )
Mmediarecorder = New Mediarecorder ();
Else
Mmediarecorder. Reset ();

// 〇 state: initial => initialized
// Set audio source as microphone, video source as camera
// Specified before settings recording-parameters or encoders, called only before setoutputformat
Mmediarecorder. setvideosource (mediarecorder. videosource. Camera );
// Mmediarecorder. setaudiosource (mediarecorder. audiosource. Mic );

// 〇 state: initialized => performanceconfigured
// Set the output format
// Three_gpp: 3GP format, h263 video arm Audio Encoding
// MPEG-4: MPEG4 media file format
// Raw_amr: only supports audio and the Audio Encoding must be amr_nb.
// Amr_nb:
// Arm_mb:
// Default:
// 3GP or MP4
// Bytes
String lvideofilefullpath;
Lvideofilefullpath = ". 3GP "; // . MP4
Mmediarecorder. setoutputformat (mediarecorder. outputformat. three_gpp );
// Mmediarecorder. setoutputformat (mediarecorder. outputformat. mpeg_4 );
Log. I (TAG, "Video: current container format:" + "3GP \ n ");

// Set audio/audio file encoding: AAC/amr_nb/amr_mb/Default
// Video: H.263, MP4-SP, or H.264
Mmediarecorder. setvideoencoder (mediarecorder. videoencoder. h264 );
// Mmediarecorder. setvideoencoder (mediarecorder. videoencoder. h263 );
// Mmediarecorder. setvideoencoder (mediarecorder. videoencoder. mpeg_4_sp );
Log. I (TAG, "Video: Current encoding format:" + "h264 \ n ");

// Audio: AMR-NB
// Mmediarecorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );

// Sets the video recording resolution. It must be placed behind the encoding and format. Otherwise, an error is returned.
// Mmediarecorder. setvideosize (176,144 );
Mmediarecorder. setvideosize (320,240 );
// Mmediarecorder. setvideosize (720,480 );
Log. I (TAG, "Video: current video size:" + "320*240 \ n ");

// Set the Frame Rate of the recorded video. It must be placed behind the encoding and format. Otherwise, an error is returned.
Mmediarecorder. setvideoframerate (frame_rate );

// Preview
Mmediarecorder. setpreviewdisplay (msurfaceholder. getsurface ());

// Set the output file mode: local storage or localsocket remote output
If (Bifnativeorremote) // Native
{
Lvideofilefullpath = strrecvideofilepath + String. valueof (system. currenttimemillis () + lvideofilefullpath;
Mrecvideofile = New File (lvideofilefullpath );
// Mmediarecorder. setoutputfile (mrecvideofile. getabsolutepath ());
Mmediarecorder. setoutputfile (mrecvideofile. getpath ()); // Called after set ** source before prepare
Log. I (TAG, "Start write into file ~ ");
}
Else // Remote
{
Mmediarecorder. setoutputfile (sender. getfiledescriptor ()); // Set output in streaming mode
Log. I (TAG, "Start send into sender ~ ");
}

//
Mmediarecorder. setmaxduration (0 ); // Called after setoutputfile before prepare, if zero or negation, disables the limit
Mmediarecorder. setmaxfilesize (0 ); // Called after setoutputfile before prepare, if zero or negation, disables the limit
Try
{
Mmediarecorder. setoninfolistener ( This );
Mmediarecorder. setonerrorlistener ( This );
// 〇 state: performanceconfigured => prepared
Mmediarecorder. Prepare ();
// 〇 state: Prepared => recording
Mmediarecorder. Start ();
Bifrecinprocess = True ;
Log. I (TAG, "initializevideo start! ");
}Catch (Exception E)
{
Releasemediarecorder ();
Finish ();
E. printstacktrace ();
}
Return True ;
} Catch (Exception E)
{
// Todo: handle exception
E. printstacktrace ();
Return False ;
}
}

 

Refs/related

See the link in the article.

 

 

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.