【流媒體】Android 即時視頻採集—MediaRecoder錄製

來源:互聯網
上載者:User
文章目錄
  • ① MediaRecoder使用
  • ② LocalSocket使用
  • ③ MediaRecorder相關
【流媒體】Android 即時視頻採集—MediaRecoder錄製

SkySeraph Mar 31st 2012

Email:zgzhaobo@gmail.com    QQ:452728574

0 囉嗦

  本篇的存在只是為系列的連貫性,其實在前面系列博文中對MediaRecoder已有用到,詳見前面Android系列博文Android(19)

1 概述

  通過Android的MediaRecorder,在SetoutputFile函數中綁定LocalSocket實現

2 知識點① MediaRecoder使用

  參考 【Android學習專題】多媒體篇:MediaRecorder 實現錄音機

② LocalSocket使用

  在手機中實現資料的邊發送邊接收。

詳細參考:http://api.apkbus.com/reference/android/net/LocalSocket.html

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

3 核心源碼① SurfaceView相關

  參考 【流媒體】Android 即時視頻採集—Camera預覽採集 和 【Android學習專題】多媒體篇:MediaRecorder 實現錄音機

② Init LocalSocket
    private void initLocalSocket()
{
receiver = new LocalSocket();
try
{
lss = new LocalServerSocket("H264");
receiver.connect(new LocalSocketAddress("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相關

參考:【Android學習專題】多媒體篇:MediaRecoder 實現錄影機  和 【Android學習專題】多媒體篇:MediaRecorder 實現錄音機

另附initiativeVideo核心模組

private boolean initializeVideo()
{
try
{
Log.i(TAG,"##initializeVideo....");
// 〇state: Initial 執行個體化MediaRecorder對象
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=>DataSourceConfigured
// 設定錄製視頻輸出格式
// THREE_GPP: 3gp格式,H263視頻ARM音頻編碼
// MPEG-4: MPEG4 media file format
// RAW_AMR: 只支援音頻且音頻編碼要求為AMR_NB
// AMR_NB:
// ARM_MB:
// Default:
// 3gp or mp4
//Android支援的音頻編解碼僅為AMR_NB;支援的視頻編解碼僅為H263,H264隻支援解碼;支援對JPEG編解碼;輸出格式僅支援.3gp和.mp4
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");

// 設定視頻/音頻檔案的編碼: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);

// 設定視頻錄製的解析度。必須放在設定編碼和格式的後面,否則報錯
//mMediaRecorder.setVideoSize(176, 144);
mMediaRecorder.setVideoSize(320, 240);
//mMediaRecorder.setVideoSize(720, 480);
Log.i(TAG, "Video:Current Video Size: "+"320*240\n");

// 設定錄製的視訊框架率。必須放在設定編碼和格式的後面,否則報錯
mMediaRecorder.setVideoFrameRate(FRAME_RATE);

// 預覽
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

// 設定輸出檔案方式: 直接本機存放區 or LocalSocket遠程輸出
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()); //設定以流方式輸出
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: DataSourceConfigured => 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

   見文中連結

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.