で http サーバー接続を使用してストリーミング ビデオを再生できますSurfaceView
。そして、メニュー項目StartRecordingとStopRecordingContextMenu
を追加して、ビデオを録画するために作成していました。しかし、呼び出すことでカメラビデオを記録できます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_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);
}
}