gpt4 book ai didi

android - 如何录制流媒体视频?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:27 25 4
gpt4 key购买 nike

我可以在 SurfaceView 中通过 http 服务器连接播放流式视频。我通过添加菜单项 StartRecordingStopRecording 创建了用于录制视频的 ContextMenu。但是我可以通过调用 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

来录制相机视频

这里我想将视频源设置为流式视频源。有没有可能这样做?

但我想在播放时录制流媒体视频。你能告诉我如何边播放边录音吗?

能否请您提供此问题的任何示例代码?

我的示例代码如下所示:

ma​​in.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayoutRoot">

<SurfaceView android:id="@+id/surfaceViewFrame" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content"></SurfaceView>

<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayoutMediaController" android:layout_height="wrap_content" android:paddingBottom="5dp"
android:paddingTop="5dp" android:layout_gravity="bottom" android:gravity="center_vertical" android:background="@color/media_controller_bg_color">

<TextView android:layout_width="wrap_content" 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_height="wrap_content"></TextView>

<SeekBar android:id="@+id/seekBarProgress" android:layout_weight="1" style="@style/MyCustomProgressStyle" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:progress="50"></SeekBar>
<TextView android:layout_width="wrap_content" 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_height="wrap_content"></TextView>

</LinearLayout>

<ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"
android:id="@+id/progressBarWait"></ProgressBar>

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" 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);

}
}

最佳答案

问题就像我在 android 上遇到的录制流媒体音频时遇到的问题。您可以尝试编写视频 header (取决于您的视频格式)以在缓冲区更新时流式传输和附加数据..将流刷新到您想要的任何地方..查看 RehearsalAudioRecorder 的工作原理。

关于android - 如何录制流媒体视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11201657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com