我正在使用mediaRecorder录制视频。 我可以开始,暂停,继续和停止录制。 我想在同一页面中播放录制的视频,而不是返回保存它的SD卡。 任何人都可以帮忙。 有些解决方案可以将视频保存在SD卡中,然后通过SD卡播放。 我正在尝试在应用内玩。 这是代码
public class VideoPreview : Fragment, ISurfaceHolderCallback, IOnClickListener,MediaRecorder.IOnInfoListener
{
private VideoView videoView = null;
private MediaController mc = null;
private ISurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mediaRecorder = new MediaRecorder();
private Camera mCamera;
private Button btnStart;
private Button btnSubmit;
private Button btnRestart;
private IList<Camera.Size> mSupportedPreviewSizes;
private Camera.Size mPreviewSize;
Android.Hardware.Camera.CameraInfo cameraInfo;
public static VideoPreview videoPreview;
public static VideoPreview NewInstance()
{
if (videoPreview == null)
{
return videoPreview = new VideoPreview();
}
else
{
return videoPreview;
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
View root = inflater.Inflate(Resource.Layout.VideoRecordFragment, null);
surfaceView =(SurfaceView)root.FindViewById(Resource.Id.surfaceView);
//mCamera = Camera.Open();
int cameraCount = 0;
// Camera cam = null;
cameraInfo = new Android.Hardware.Camera.CameraInfo();
cameraCount = Android.Hardware.Camera.NumberOfCameras;
int orientation = CalculatePreviewOrientation(cameraInfo, Activity.WindowManager.DefaultDisplay.Rotation);
for (int camIdx = 0; camIdx < cameraCount; camIdx++)
{
Android.Hardware.Camera.GetCameraInfo(camIdx, cameraInfo);
if (cameraInfo.Facing == Android.Hardware.Camera.CameraInfo.CameraFacingFront)
{
try
{
mCamera = Android.Hardware.Camera.Open(camIdx);
mCamera.SetDisplayOrientation(90);
}
catch (Exception e)
{
//Log.Error(TAG, "Camera failed to open: " + e.Message);
}
}
}
// mCamera.setDisplayOrientation(90);
surfaceHolder = surfaceView.Holder;
surfaceHolder.AddCallback(this);
surfaceHolder.SetType(SurfaceType.PushBuffers);
btnStart = (Button)root.FindViewById(Resource.Id.btnStart);
btnSubmit = (Button)root.FindViewById(Resource.Id.btnSubmit);
btnRestart = (Button)root.FindViewById(Resource.Id.btnReset);
btnStart.SetOnClickListener(this);
btnSubmit.SetOnClickListener(this);
btnRestart.SetOnClickListener(this);
//return base.OnCreateView(inflater, container, savedInstanceState);
return root;
}
protected void startRecording()
{
int orientation;
if (mCamera == null)
{
int cameraCount = 0;
// Camera cam = null;
cameraInfo = new Android.Hardware.Camera.CameraInfo();
cameraCount = Android.Hardware.Camera.NumberOfCameras;
orientation = CalculatePreviewOrientation(cameraInfo, Activity.WindowManager.DefaultDisplay.Rotation);
for (int camIdx = 0; camIdx < cameraCount; camIdx++)
{
Android.Hardware.Camera.GetCameraInfo(camIdx, cameraInfo);
if (cameraInfo.Facing == Android.Hardware.Camera.CameraInfo.CameraFacingFront)
{
try
{
mCamera = Android.Hardware.Camera.Open(camIdx);
mCamera.SetDisplayOrientation(90);
}
catch (Exception e)
{
//Log.Error(TAG, "Camera failed to open: " + e.Message);
}
}
}
}
// mCamera = Camera.Open();
orientation = CalculatePreviewOrientation(cameraInfo, Activity.WindowManager.DefaultDisplay.Rotation);
File sdCard = Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures);
File dir = new File(sdCard.AbsolutePath + "/SignIn/SignIn Videos");
if (!dir.Exists())
{
dir.Mkdirs();
}
Date date = new Date();
String fileName = "/rec" + date.ToString().Replace(" ", "_").Replace(":", "_") + ".mp4";
File file = new File(dir, fileName);
// mCamera.GetParameters().SetRotation(orientation);
mediaRecorder = new MediaRecorder();
mCamera.Lock();
mCamera.Unlock();
// Please maintain sequence of following code.
// If you change sequence it will not work.
mediaRecorder.SetCamera(mCamera);
mediaRecorder.SetVideoSource(VideoSource.Camera);
mediaRecorder.SetAudioSource(AudioSource.Camcorder);
// mediaRecorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.High));
// mediaRecorder.SetProfile(CamcorderProfile.Get(CamcorderQuality.High));
mediaRecorder.SetOutputFormat(OutputFormat.Default);
mediaRecorder.SetVideoEncoder(VideoEncoder.Default);
mediaRecorder.SetAudioEncoder(AudioEncoder.Default);
mediaRecorder.SetOutputFile(dir + fileName);
mediaRecorder.SetMaxDuration(10000);
mediaRecorder.SetOnInfoListener(this);
mediaRecorder.SetPreviewDisplay(surfaceHolder.Surface);
mediaRecorder.SetOrientationHint(270);
mediaRecorder.Prepare();
mediaRecorder.Start();
refreshGallery(file);
}
private void refreshGallery(File file)
{
Intent mediaScanIntent = new Intent(
Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(file));
Activity.SendBroadcast(mediaScanIntent);
}
protected void stopRecording()
{
if (mediaRecorder != null)
{
mediaRecorder.Stop();
mediaRecorder.Release();
releaseCamera();
// mCamera.lock();
}
}
private void releaseMediaRecorder()
{
if (mediaRecorder != null)
{
mediaRecorder.Reset(); // clear recorder configuration
mediaRecorder.Release(); // release the recorder object
}
}
private void releaseCamera()
{
if (mCamera != null)
{
mCamera.Release(); // release the camera for other applications
mCamera = null;
}
}
public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Android.Graphics.Format format, int width, int height)
{
// throw new NotImplementedException();
}
public void SurfaceCreated(ISurfaceHolder holder)
{
//throw new NotImplementedException();
if (mCamera != null)
{
//Parameters param = mCamera.GetParameters();
//mCamera.SetParameters(param);
//mCamera.SetDisplayOrientation(90);
//Log.i("Surface", "Created");
}
else
{
Toast.MakeText(Activity, "Camera not available!",
ToastLength.Long).Show();
// Finish();
}
}
public void SurfaceDestroyed(ISurfaceHolder holder)
{
mCamera.StopPreview();
releaseCamera();
}
public void OnClick(View v)
{
switch (v.Id)
{
case Resource.Id.btnStart:
if (btnStart.Text.ToLower().ToString().Equals("start"))
{
btnStart.SetText("Pause", TextView.BufferType.Normal);
btnSubmit.Visibility = ViewStates.Visible;
btnRestart.Visibility = ViewStates.Visible;
try
{
startRecording();
}
catch (IOException e)
{
// String message = e.getMessage();
// Log.i(null, "Problem " + message);
mediaRecorder.Release();
// e.printStackTrace();
}
}
else if (btnStart.Text.ToLower().ToString().Equals("pause"))
{
btnStart.SetText("Resume", TextView.BufferType.Normal);
try
{
mediaRecorder.Pause();
}
catch (IOException e)
{
// String message = e.getMessage();
// Log.i(null, "Problem " + message);
mediaRecorder.Release();
// e.printStackTrace();
}
}
else if (btnStart.Text.ToLower().ToString().Equals("resume"))
{
btnStart.SetText("Pause", TextView.BufferType.Normal);
try
{
mediaRecorder.Resume();
}
catch (IOException e)
{
// String message = e.getMessage();
// Log.i(null, "Problem " + message);
mediaRecorder.Release();
// e.printStackTrace();
}
}
break;
case Resource.Id.btnSubmit:
try
{
btnStart.SetText("Start", TextView.BufferType.Normal);
btnStart.Visibility = ViewStates.Visible;
btnSubmit.Visibility = ViewStates.Gone;
btnRestart.Visibility = ViewStates.Gone;
mediaRecorder.Stop();
mediaRecorder.Release();
mediaRecorder = null;
}
catch(Exception ex)
{
mediaRecorder.Release();
}
break;
case Resource.Id.btnReset:
try
{
btnStart.SetText("Pause", TextView.BufferType.Normal);
mediaRecorder.Reset();
}
catch (Exception ex)
{
mediaRecorder.Release();
}
break;
default:
break;
}
}
public static int CalculatePreviewOrientation(Camera.CameraInfo info, SurfaceOrientation rotation)
{
int degrees = 0;
switch (rotation)
{
case SurfaceOrientation.Rotation0:
degrees = 0;
break;
case SurfaceOrientation.Rotation90:
degrees = 90;
break;
case SurfaceOrientation.Rotation180:
degrees = 180;
break;
case SurfaceOrientation.Rotation270:
degrees = 270;
break;
}
int result;
if (info.Facing == CameraFacing.Front)
{
result = (info.Orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
}
else
{ // back-facing
result = (info.Orientation - degrees + 360) % 360;
}
return result;
}
public void OnInfo(MediaRecorder mr, [GeneratedEnum] MediaRecorderInfo what, int extra)
{
mediaRecorder.Pause();
btnSubmit.Visibility = ViewStates.Visible;
btnRestart.Visibility = ViewStates.Visible;
btnStart.Visibility = ViewStates.Gone;
//if (what. == MediaRecorder)
//{
// Log.v("VIDEOCAPTURE", "Maximum Duration Reached");
// mr.stop();
//}
}
}
答案 0 :(得分:0)
VideoView mVideoView;
private void playVideo(object sender, EventArgs e)
{
try
{
mVideoView.SetVideoPath(dir + "/rec" + videoFileName + ".mp4");
mVideoView.SetMediaController(new MediaController(Activity));
mVideoView.RequestFocus();
mVideoView.Start();
//StopTimer();
//CreateTimer();
}
catch (IOException ex)
{
// String message = e.getMessage();
// Log.i(null, "Problem " + message);
StopTimer();
mediaRecorder.Release();
// e.printStackTrace();
}
}
答案 1 :(得分:0)
您可以在android中使用视频视图:
添加一个像这样的全局字段:
VideoView mVideoView;
找到相同的引用,如下所示:
mVideoView=this.FindViewById<VideoView>(Resource.Id.mvidId); //Activity init
mVideoView=View.FindViewById<VideoView>(Resource.Id.mvidId); //Fragment init
播放类似这样的视频:
private void playVideo(object sender, EventArgs e)
{
try
{
mVideoView.SetVideoPath(**VideoPath**);
mVideoView.SetMediaController(new MediaController(Activity));
mVideoView.RequestFocus();
mVideoView.Start();
}
catch (IOException ex)
{
StopTimer();
mediaRecorder.Release();
}
}