如何录制流媒体视频?
Posted
技术标签:
【中文标题】如何录制流媒体视频?【英文标题】:How to Record Streaming Video? 【发布时间】:2012-06-27 10:27:39 【问题描述】:我可以通过SurfaceView
中的 http 服务器连接播放流媒体视频。我正在创建ContextMenu
,通过添加菜单项StartRecording 和StopRecording 来录制视频。但是我可以通过调用mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
来录制相机视频
这里我想将视频源设置为流式视频源。有没有可能这样做?
但我想在播放时录制流媒体视频。你能告诉我在播放的时候怎么录音吗?
请您提供此问题的任何示例代码吗?
我的示例代码如下所示:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_
android:layout_>
<FrameLayout android:layout_ android:layout_ android:id="@+id/frameLayoutRoot">
<SurfaceView android:id="@+id/surfaceViewFrame" android:layout_gravity="center" android:layout_ android:layout_></SurfaceView>
<LinearLayout android:layout_ android:id="@+id/linearLayoutMediaController" android:layout_ android:paddingBottom="5dp"
android:paddingTop="5dp" android:layout_gravity="bottom" android:gravity="center_vertical" android:background="@color/media_controller_bg_color">
<TextView android:layout_ android:id="@+id/textViewPlayed" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:textColor="@color/media_controller_text_color" android:textStyle="bold" android:text="0:00:00" android:padding="0dp" android:textSize="13sp" android:gravity="center"
android:layout_></TextView>
<SeekBar android:id="@+id/seekBarProgress" android:layout_weight="1" style="@style/MyCustomProgressStyle" android:layout_
android:layout_ android:progress="50"></SeekBar>
<TextView android:layout_ android:id="@+id/textViewLength" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
android:textColor="@color/media_controller_text_color" android:textStyle="bold" android:text="0:00:00" android:textSize="13sp" android:padding="0dp" android:gravity="center"
android:layout_></TextView>
</LinearLayout>
<ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_ android:layout_ android:layout_gravity="center"
android:id="@+id/progressBarWait"></ProgressBar>
<ImageView android:layout_ android:layout_ android:id="@+id/imageViewPauseIndicator" android:layout_gravity="center"
android:src="@drawable/pause_button"></ImageView>
</FrameLayout>
MediaPlayerDemo.java:
public class MediaPlayerDemo_Video extends Activity implements
OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnClickListener, OnSeekCompleteListener,
Callback, OnSeekBarChangeListener, AnimationListener
private String path2 = "http://commonsware.com/misc/test2.3gp";
private String path = "";
private static final String TAG = "MediaPlayerDemo";
private MediaRecorder mediaRecorder;
private MediaPlayer mMediaPlayer;
private SurfaceView surfaceViewFrame;
private SurfaceHolder surfaceHolder;
// private String path;
private Bundle extras;
private Animation hideMediaController;
private LinearLayout linearLayoutMediaController;
private SeekBar seekBarProgress;
private TextView textViewPlayed;
private TextView textViewLength;
private ProgressBar progressBarWait;
private ImageView imageViewPauseIndicator;
private Timer updateTimer;
AudioManager volume;
int keyCode = 0;
KeyEvent keyEvent;
private MediaRecorder mVideoRecorder = null;
private Camera mCamera;
@Override
public void onCreate(Bundle icicle)
super.onCreate(icicle);
setContentView(R.layout.mediaplayer_2);
linearLayoutMediaController = (LinearLayout) findViewById(R.id.linearLayoutMediaController);
linearLayoutMediaController.setVisibility(View.GONE);
hideMediaController = AnimationUtils.loadAnimation(MediaPlayerDemo_Video.this, R.anim.disapearing);
hideMediaController.setAnimationListener(this);
imageViewPauseIndicator = (ImageView) findViewById(R.id.imageViewPauseIndicator);
imageViewPauseIndicator.setVisibility(View.GONE);
textViewPlayed = (TextView) findViewById(R.id.textViewPlayed);
textViewLength = (TextView) findViewById(R.id.textViewLength);
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
surfaceViewFrame.setOnClickListener(this);
surfaceViewFrame.setClickable(false);
seekBarProgress = (SeekBar) findViewById(R.id.seekBarProgress);
seekBarProgress.setOnSeekBarChangeListener(this);
// seekBarProgress.setProgress(0);
progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);
volume = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = volume.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = volume.getStreamVolume(AudioManager.STREAM_MUSIC);
seekBarProgress.setMax(maxVolume);
seekBarProgress.setProgress(curVolume);
surfaceHolder = surfaceViewFrame.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnSeekCompleteListener(this);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
if (mMediaPlayer != null)
if (!mMediaPlayer.isPlaying())
imageViewPauseIndicator.setVisibility(View.VISIBLE);
@Override
public boolean onCreateOptionsMenu(Menu menu)
menu.add(0, 0, 0, "StartRecording");
menu.add(0, 0, 0, "StopRecording");
return super.onCreateOptionsMenu(menu);
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case 0:
if (mediaRecorder == null)
mediaRecorder = new MediaRecorder(); // Works well
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFile("/sdcard/DCIM/Camera/StreamLive.3gp");
mediaRecorder.setMaxDuration(-1);
try
mediaRecorder.prepare();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
break;
case 1:
mediaRecorder.stop();
mediaRecorder.release();
break;
default:
break;
return super.onOptionsItemSelected(item);
private void playVideo()
try
mMediaPlayer.setDataSource(path);
catch (IllegalArgumentException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
mMediaPlayer.setDisplay(surfaceHolder);
try
mMediaPlayer.prepare();
catch (IllegalStateException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k)
Log.d(TAG, "surfaceChanged called");
public void surfaceDestroyed(SurfaceHolder surfaceholder)
Log.d(TAG, "surfaceDestroyed called");
public void surfaceCreated(SurfaceHolder holder)
Log.d(TAG, "surfaceCreated called");
playVideo();
public void onPrepared(MediaPlayer mediaplayer)
Log.i(TAG, "========== onPrepared ===========");
int duration = mediaplayer.getDuration() / 1000; // duration in seconds
seekBarProgress.setMax(duration);
textViewLength.setText(Utils.durationInSecondsToString(duration));
progressBarWait.setVisibility(View.GONE);
// Get the dimensions of the video
int videoWidth = mMediaPlayer.getVideoWidth();
int videoHeight = mMediaPlayer.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
Log.i(TAG, "VIDEO SIZES: W: " + videoWidth + " H: " + videoHeight + " PROP: " + videoProportion);
// Get the width of the screen
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
Log.i(TAG, "VIDEO SIZES: W: " + screenWidth + " H: " + screenHeight + " PROP: " + screenProportion);
// Get the SurfaceView layout parameters
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion)
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
else
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
// Commit the layout parameters
surfaceViewFrame.setLayoutParams(lp);
// Start video
if (!mMediaPlayer.isPlaying())
mMediaPlayer.start();
updateMediaProgress();
linearLayoutMediaController.setVisibility(View.VISIBLE);
hideMediaController();
surfaceViewFrame.setClickable(true);
public void onCompletion(MediaPlayer mp)
Log.d(TAG, "onCompletion called");
mp.stop();
if (updateTimer != null)
updateTimer.cancel();
// mp.seekTo(0);
// finish();
public void onBufferingUpdate(MediaPlayer mp, int percent)
Log.d(TAG, "onBufferingUpdate percent:" + percent);
int progress = (int) ((float) mp.getDuration() * ((float) percent / (float) 100));
seekBarProgress.setSecondaryProgress(progress / 1000);
【问题讨论】:
您是否找到了如何在播放视频时录制视频,因为我也在寻找相同的内容。 【参考方案1】:问题就像我在 android 上录制流式音频时遇到的问题。 您可以尝试写入视频标头(取决于您的视频格式)以在缓冲区更新时流式传输和附加数据..flush 流到您想要的任何位置.. 查看 RehearsalAudioRecorder 的工作原理。
【讨论】:
以上是关于如何录制流媒体视频?的主要内容,如果未能解决你的问题,请参考以下文章
javaCV开发详解之12:视频文件转apng动态图片实现,视频生成apng,也支持摄像机桌面屏幕流媒体等视频源录制apng动态图